Page 1 of 1

PHP/MySQL;php Different Logins to direct to different pages?

Posted: Tue Dec 09, 2008 5:48 pm
by cedartree
I have created a login system on my webpage.
currently the username/password loads a specific page once logged in.
i can create different usernames and passwords no problem.

how can i get each different user account to load in a seperate page???
example:
USER1 logs in and loads PAGE1.php
USER2 logs in and loads PAGE2.php
USER3 logs in and loads PAGE3.php, etc etc

here is my code for the login page:

Code: Select all

 
<?php
ob_start();
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="members"; // 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");
 
// Define $myusername and $mypassword
$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_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
 
ob_end_flush();
?>
 

Re: PHP/MySQL;php Different Logins to direct to different pages?

Posted: Tue Dec 09, 2008 7:49 pm
by novice4eva
you want to create different welcome page for each user?? I don't think that is what you are after...or is it!! :dubious:

Re: PHP/MySQL;php Different Logins to direct to different pages?

Posted: Tue Dec 09, 2008 10:47 pm
by cedartree
yep! that is what i want because the data that gets filled out in the form has to go to that specific person.

unless there is another way......

Re: PHP/MySQL;php Different Logins to direct to different pages?

Posted: Tue Dec 09, 2008 10:54 pm
by requinix
Add a field to the table so you know which page each user should go to. Then use header to redirect them.

Re: PHP/MySQL;php Different Logins to direct to different pages?

Posted: Wed Dec 10, 2008 8:38 am
by cedartree
please let me know if i am doing this correctly. i am still learning and trying very hard :)

so in my "members" table i will have to create:

Code: Select all

 
INSERT INTO `members` VALUES (1, 'USER1', 'pw');
INSERT INTO `members` VALUES (2, 'USER2', 'pw');
(and so on and so on for other members)
 
and then in my checklogin.php page should it look like:

Code: Select all

 
 
// 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 "PAGE1.php"
session_register("myusername");
session_register("mypassword");
header("location:PAGE1.php");
}
else {
echo "Wrong Username or Password";
}
 
if($count==2){
// Register $myusername, $mypassword and redirect to file "PAGE2.php"
session_register("myusername");
session_register("mypassword");
header("location:PAGE2.php");
}
else {
echo "Wrong Username or Password";
}
?>
 

Re: PHP/MySQL;php Different Logins to direct to different pages?

Posted: Wed Dec 10, 2008 9:32 am
by cedartree
yea i just tried the above post i left but that did not work.

Re: PHP/MySQL;php Different Logins to direct to different pages?

Posted: Wed Dec 10, 2008 8:59 pm
by novice4eva
No no its not like that, when you validate a user against his/her username and password, if correct the query is supposed to return only one row associated with the user so 'if valid' the count will always equal to 1 else its 0, if it's returning more than 1 either your query is missing something or your table is missing parent key.
So it would be something like this:

Code: Select all

 
if($count==1){
 
$_SESSION["myusername"] = $_POST['name'];//session_register is kinda old and deprecated i think
$_SESSION["mypassword"] = $_POST['password'];
 
switch($_SESSION["myusername"])
{
case 'user1':
header("location:PAGE1.php");
break;
 
case 'user2':
header("location:PAGE2.php");
break;
...
....
}
}
else {
echo "Wrong Username or Password";
}