|
Posted by Jack on 07/03/07 22:07
Hi everyone,
I am stuck on something really stupid and simple. Here is what I am
trying to do. Login, check the username and password against db and if
it checks out then check the logged variable session and pass the user
to the next page. Here is a sample of the login process page:
$user = 'test';
$pass = sha1('testpass');
//set the database connection variables
$hostname = "localhost";
$dbusername = "newuser";
$dbpassword = "newpassword";
$dbh = mysql_connect($hostname, $dbusername, $dbpassword) or
die("Unable to connect to the database");
mysql_select_db("seandb", $dbh);
$result = mysql_query("select * from users where username='$user' AND
password='$pass'", $dbh);
//check that at least one row was returned
$rowCheck = mysql_num_rows($result);
if($rowCheck > 0){
while($row = mysql_fetch_array($result)){
//start the session
session_start();
session_register('sname');
$sname = $user;
//successful login code will go here...
header( "Location: main.php" );
}
}else {
include 'index.php';
printf("Incorrect Login...");
}
Then on the Main page I want to be able check the variable sname and
if it is set then say hi.
For some reason this doesn't happen. The variable gets assigned a
variable but once it is passed over to main.php, the session variable
is not there anymore. Here is the code for main.php:
if (!empty($_SESSION['sname'])) {
?>
<p>Welcome</p><?php echo($_SESSION['sname']);?>
<?php
}else{
printf("failed...");
//header( "Location: index.php" );
}
For some reason I always get the "failed" message
any ideas...?
Thanks
J
Navigation:
[Reply to this message]
|