soccerdudez8
03-04-2004, 10:26 PM
I'm new to PHP, and i'm having troubles with probably one of the easiest things, sessions. I'm trying to create a user login script that reads info from a database and puts it into a session. look at the code for yourself.
<?php
if(!empty($_POST["username"]) && !empty($_POST["password"])){
login_user(login_reformat($username),login_reformat($password));
}
function login_reformat($str){
$str = trim($str);
$str = addslashes($str);
$str = EscapeShellCmd($str);
$str = substr($str,0,20);
return $str;
}
$login_error =false;
function login_user($user,$pass){
global $login_error;
$conn = dbconn(); //database connection function
$query = "..."; //query goes here
if(!($result = mysql_query($query, $conn)))
mysqlerror(); //custom error handling
if(mysql_num_rows($result) !== 1)
$login_error = true;
else{
$login_error = false;
if(!($row = @ mysql_fetch_array($result)))
mysqlerror();
}
if(!($user==$row["username"]) || !($pass==$row["password"]))
$login_error = true;
else{
$row["logins"]++;
$curdate = date("Y") . "-" . date("m") . "-" . date("d");
$query = ".."; //query for updating number of logins
if(!(mysql_query($query,$conn)))
mysqlerror();
session_start();
session_register("user");
$user = array(username=> $row["username"], first_name => $row["first_name"], last_name => $row["last_name"],
email => $row["email"], security_level => $row["security_level"],
options => serialize($row["options"]), logged_in =>true);
header("location: msg.php?txt=" . urlencode("Login successful!
Welcome {$row["first_name"]} {$row["last_name"]}!") . "&r=" . urlencode("index.php?" . SID));
exit;
}
?>
what happens is that the code reads from the database fine, supposedly starts the session, and returns of a message page, the only problem is that the session does NOT start. its true. i ran a script in "index.php" so that it displayed all off the contents of the session, and turned up empty. i've looked at my host's php.ini settings and it allows sessions (I'm at www.freepgs.com). is it possible that i am trying to access the session information incorrectly? i don't think so, but this is what i do...
<?php
$user_logged_in = ($_SESSION["user"]["logged_in"] === true) ? true : false;
$admin_security = (strtolower($_SESSION["user"]["security_level"]) == "secret code here") ? true : false;
?>
what am i doing wrong?
<?php
if(!empty($_POST["username"]) && !empty($_POST["password"])){
login_user(login_reformat($username),login_reformat($password));
}
function login_reformat($str){
$str = trim($str);
$str = addslashes($str);
$str = EscapeShellCmd($str);
$str = substr($str,0,20);
return $str;
}
$login_error =false;
function login_user($user,$pass){
global $login_error;
$conn = dbconn(); //database connection function
$query = "..."; //query goes here
if(!($result = mysql_query($query, $conn)))
mysqlerror(); //custom error handling
if(mysql_num_rows($result) !== 1)
$login_error = true;
else{
$login_error = false;
if(!($row = @ mysql_fetch_array($result)))
mysqlerror();
}
if(!($user==$row["username"]) || !($pass==$row["password"]))
$login_error = true;
else{
$row["logins"]++;
$curdate = date("Y") . "-" . date("m") . "-" . date("d");
$query = ".."; //query for updating number of logins
if(!(mysql_query($query,$conn)))
mysqlerror();
session_start();
session_register("user");
$user = array(username=> $row["username"], first_name => $row["first_name"], last_name => $row["last_name"],
email => $row["email"], security_level => $row["security_level"],
options => serialize($row["options"]), logged_in =>true);
header("location: msg.php?txt=" . urlencode("Login successful!
Welcome {$row["first_name"]} {$row["last_name"]}!") . "&r=" . urlencode("index.php?" . SID));
exit;
}
?>
what happens is that the code reads from the database fine, supposedly starts the session, and returns of a message page, the only problem is that the session does NOT start. its true. i ran a script in "index.php" so that it displayed all off the contents of the session, and turned up empty. i've looked at my host's php.ini settings and it allows sessions (I'm at www.freepgs.com). is it possible that i am trying to access the session information incorrectly? i don't think so, but this is what i do...
<?php
$user_logged_in = ($_SESSION["user"]["logged_in"] === true) ? true : false;
$admin_security = (strtolower($_SESSION["user"]["security_level"]) == "secret code here") ? true : false;
?>
what am i doing wrong?