So I want the code to allow the user to login and then direct them to their page, I'm just not sure how I can do that. Here's the table and code I have, is there a way to make "header("location:/instacp.htm");" pull the location from the table in the database?

Table:
id username password location
1 12345 123456 /instacp.htm

<?php
$host="localhost";
$username="blaa";
$password="blaa";
$db_name="blaa_members";
$tbl_name="members";


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$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);


$count=mysql_num_rows($result);


if($count==1){
session_register("myusername");
session_register("mypassword"); 
header("location:/instacp.htm");
}
else {
echo "Wrong Username or Password";
}
?>

Hi bdcd,

So I want the code to allow the user to login and then direct them to their page, I'm just not sure how I can do that. Here's the table and code I have, is there a way to make "header("location:/instacp.htm");" pull the location from the table in the database?

Table:
id username password location
1 12345 123456 /instacp.htm

This should do the trick (note: this code is untested):

<?php
$host="localhost";
$username="blaa";
$password="blaa";
$db_name="blaa_members";
$tbl_name="members";


mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 


$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);

$row=mysql_fetch_assoc($result);

if($row!=false){
	session_register("myusername");
	session_register("mypassword"); 
	header("location: ".$row['location']);
}
else {
	echo "Wrong Username or Password";
}
?>

More information on using mysql_fetch_assoc() can be found here - http://www.php.net/manual/en/function.mysql-fetch-assoc.php

Adnan

Thank you it works now, thanks for the link as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.