How to set and get request attributes in php5

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kittu_chaitu
Forum Newbie
Posts: 4
Joined: Tue Jul 12, 2011 10:03 am

How to set and get request attributes in php5

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to set and get request attributes in php5

Post 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!";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kittu_chaitu
Forum Newbie
Posts: 4
Joined: Tue Jul 12, 2011 10:03 am

Re: How to set and get request attributes in php5

Post 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?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to set and get request attributes in php5

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
kittu_chaitu
Forum Newbie
Posts: 4
Joined: Tue Jul 12, 2011 10:03 am

Re: How to set and get request attributes in php5

Post by kittu_chaitu »

Thank you Mr. AbraCadaver.
kittu_chaitu
Forum Newbie
Posts: 4
Joined: Tue Jul 12, 2011 10:03 am

Re: How to set and get request attributes in php5

Post by kittu_chaitu »

But what if cookies are disabled at client side? Session will not work. How to handle such situations?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to set and get request attributes in php5

Post 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
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply