Page 1 of 1

Problem with re-directing user to a specific link.

Posted: Tue Aug 05, 2008 8:28 am
by jerryhughes
Hi,

I've been building a client log-in system recently, and I've managed to get ther user to register and then sign in successfully. Now with multiple users, Im trying to re-direct a different user to a different page.

The full page of code im working with is the following;

Code: Select all

 
<?php
$host=""; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name=""; // Database name 
$tbl_name=""; // Table name 
 
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");
 
// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 
 
// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
 
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);
 
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
 
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
 
 
if($myusername = "john") $url = "page1.php";
else 
if($myusername = "adam") $url = "page2.php"; 
header("location: ".$url);
}
else {
echo "Wrong Username or Password";
}
?>
 
 
Now the key to it is at the bottom of the page

Code: Select all

 
 
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
 
 
if($myusername = "john") $url = "page1.php";
else 
if($myusername = "adam") $url = "page2.php"; 
header("location: ".$url);
 
When i log in with either username adam or john it takes them both to page1.php.

I tried using the unset variable function in between them but then that just sent both users to page2.php

What I want is for when john signs in he gets sent to page1.php and when adam signs in he gets sent to page2.php.

Does anyone have any ideas where I'm going wrong here?

Thanks,

Jerry

Re: Problem with re-directing user to a specific link.

Posted: Tue Aug 05, 2008 8:33 am
by Dynamis
In the if statement, something I see right away is you are using = instead of ==. We've all been there :). Hope that fixes it. I also changed the else, if, to else if. Not sure if this change affects you, just seems more logical that way.

Updated code:

Code: Select all

 
 
$_SESSION['myusername'] = $myusername;
$_SESSION['mypassword'] = $mypassword;
 
 
if($myusername == "john") $url = "page1.php";
else if($myusername == "adam") $url = "page2.php"; 
header("location: ".$url);
 

Re: Problem with re-directing user to a specific link.

Posted: Tue Aug 05, 2008 8:56 am
by jerryhughes
ahhhh sweet man!! thanks so much, I assume I can now use this technique for multiple amounts of users, just keep adding new else if statements?

cheers for the quick reply too!!

Re: Problem with re-directing user to a specific link.

Posted: Tue Aug 05, 2008 9:04 am
by Dynamis
jerryhughes wrote:I assume I can now use this technique for multiple amounts of users, just keep adding new else if statements?
That is correct. If you have a large number of users though, I would take time to rethink your structure and find a better way than redirecting all of your users (such as one page displaying different information based on the user or user group).