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

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
cedartree
Forum Newbie
Posts: 10
Joined: Thu Dec 04, 2008 1:35 pm

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

Post 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();
?>
 
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

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

Post 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:
cedartree
Forum Newbie
Posts: 10
Joined: Thu Dec 04, 2008 1:35 pm

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

Post 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......
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

Add a field to the table so you know which page each user should go to. Then use header to redirect them.
cedartree
Forum Newbie
Posts: 10
Joined: Thu Dec 04, 2008 1:35 pm

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

Post 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";
}
?>
 
cedartree
Forum Newbie
Posts: 10
Joined: Thu Dec 04, 2008 1:35 pm

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

Post by cedartree »

yea i just tried the above post i left but that did not work.
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

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

Post 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";
}
 
Post Reply