Login Re-direct

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
amijic
Forum Newbie
Posts: 3
Joined: Thu Jan 26, 2012 11:07 pm

Login Re-direct

Post by amijic »

Hi all,

I have created a basic login PHP script that requires a username and password. The script is written using If statements. What I would like to happen is that once a user logs in that he/she be automatically re-directed to a memmebr page. Right now the user needs to click a word/button in order to proceed I would like to skip that step and have them go straight to the member page after they press login.

Basically I am looking to replace the following lines with a re-direct code

if ($username==$dbusername&&$password==$dbpassword)
{
echo "Ckick <a href='Success.php'>here</a> to access members page";
$_SESSION['username']=$dbusername;
}

Thank You
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Login Re-direct

Post by social_experiment »

Code: Select all

<?php
if ($username==$dbusername&&$password==$dbpassword) {
   session_regenerate_id();
   // set session variables here
   session_write_close();
   header('location: your/page.php');
   exit();   
}
?>
Hth
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Hitman47
Forum Newbie
Posts: 6
Joined: Thu Jan 26, 2012 5:58 pm

Re: Login Re-direct

Post by Hitman47 »

The above will work. You could also use javascript.

The only thing I want to add is try to write your code in the order you want it to actually execute, it can prevent headaches in the future. In your first code snippet you first echo the actual text with the link THEN you set the session variables. Naturally those two operations should be reversed. Just some thoughts, I suppose.
Post Reply