Page 1 of 1

How to set and get request attributes in php5

Posted: Tue Jul 12, 2011 10:10 am
by kittu_chaitu
Hi all, I am Chaitanya. I am new to php. I am trying to build a login example using php. The user sends login credentials from login.php to logincheck.php. The logincheck.php page checks the userid and password in the database. If login is valid he is redirected to home.php or else he is redirected to login.php.

What I want to do is I want to set a jsp style request attribute in the logincheck.php page. When the user is redirected to login.php I want to get the request attribute and display a message that the login credentials are not valid.

Ho can I do this. Any help is highly appreciated. Thank you all in advance.

Re: How to set and get request attributes in php5

Posted: Tue Jul 12, 2011 11:02 am
by AbraCadaver
Two easy ways.

1. Set a session var in logincheck.php and then check for it on login.php and if it is set display it.
2. When you redirect because of a failed login, redirect like so: Location: login.php?status=0, then on the next page check $_GET['status'] and decide whether to display a message.
3. Have the login and logincheck in the same page that submits to itself. Then if the login fails, just display the message and redisplay the form.

I normally opt for #1 or combination of #1 and #3 as there are many times I want to display a message such as "your email has been sent", "your comment has been posted", etc. So in a header.php that is included at the top of every page, or in the specific pages, you would do this:

Code: Select all

session_start();
if(isset($_SESSION['message'})) {
   echo $message;
   unset($_SESSION['message']);
}
And on pages to set a message, do:

Code: Select all

// assuming header.php has already been included so the session is started
$_SESSION['message'] = "Your username or password is incorrect!";

Re: How to set and get request attributes in php5

Posted: Tue Jul 12, 2011 11:16 am
by kittu_chaitu
Thanks you Mr. AbraCadaver.

I am going with the SESSION variable. But setting a session and removing it, is it a good idea?

Re: How to set and get request attributes in php5

Posted: Tue Jul 12, 2011 11:46 am
by AbraCadaver
kittu_chaitu wrote:Thanks you Mr. AbraCadaver.

I am going with the SESSION variable. But setting a session and removing it, is it a good idea?
Sure, why not?

Re: How to set and get request attributes in php5

Posted: Wed Jul 13, 2011 4:05 am
by kittu_chaitu
Thank you Mr. AbraCadaver.

Re: How to set and get request attributes in php5

Posted: Wed Jul 13, 2011 4:55 am
by kittu_chaitu
But what if cookies are disabled at client side? Session will not work. How to handle such situations?

Re: How to set and get request attributes in php5

Posted: Wed Jul 13, 2011 9:31 am
by AbraCadaver
If cookies are disabled then you are not going to be able to track a user's state via sessions or any other way unless you propagate the session id via all of the URLs. If someone has cookies disabled then they shouldn't expect to be able to login to very many sites on the Internet. I wouldn't worry about it, but you can try this: http://us3.php.net/manual/en/session.idpassing.php