Forum Moderators: coopster

Message Too Old, No Replies

help with login script - sessions

         

adammc

2:13 am on May 29, 2006 (gmt 0)

10+ Year Member



Hi folks,

I am having trouble with my login script :(
I am trying to get the script to extract the state for each member as they login and store it in the session with the username.

I have tried echoing the state variable and the session variable (state) with no result.

[PHP]
$conn = @mysql_connect("localhost","user","pass") or die("Err:Conn");
$rs = @mysql_select_db("database",$conn) or die("Err:Db");

$sql = "select * from members where username='" . mysql_real_escape_string($username) . "' and password = '" . mysql_real_escape_string($password) . "'";
$sql_result = mysql_query($sql, $conn) or die(mysql_error());
if (mysql_num_rows($sql_result) > 0) {

$state = $row["state"];


$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = $_POST['username'];
$_SESSION['state'] = $row["state"];

mysql_close($conn);
header("Location: our-products.php?state=$state");
exit();

} else {

header("Location: login.html");
exit();
}
}
[/PHP]

Any help would be GREATLY appreciated :)

eelixduppy

2:33 am on May 29, 2006 (gmt 0)



You didn't define $row. Try something like this:

if ($row=mysql_fetch_array($sql_result)) {

$state = $row["state"];

$_SESSION['loggedIn'] = 1;
$_SESSION['username'] = $_POST['username'];
$_SESSION['state'] = $state;

mysql_close($conn);
header("Location: our-products.php?state=$state");
exit();

}

adammc

4:17 am on May 29, 2006 (gmt 0)

10+ Year Member



thank you eelixduppy.
You have saved the day again :)

wrightee

5:57 pm on May 29, 2006 (gmt 0)

10+ Year Member



Just a note, after setting $_SESSION and then redirecting using header("Location: our-products.php?state=$state") - $_SESSION will not be set on the our-products.php page until it is reloaded. You can solve this if it causes a problem by echo'ing a meta refresh with the URL instead.