Topic awaiting preservation: Basic PHP Session problem (Page 1 of 1) |
|
---|---|
Neurotic (0) Inmate Newly admitted From: |
posted 08-25-2006 01:13
Hi, very n00b to php but not to ozone. I used to have an account but it was probably removed anyways I was doing basic script and was trying to get my session to work. I think I'm placing them in the wrong way. Basically here's what i have (roughly): code: <? session_start(); if (!isset($username) && !isset($password)) { //run the login form } ?>
code: <? include ("Login.php"); echo '<a href="edit.php">Edit</a>'; ?>
code: <? session_start(); echo $username; ?>
|
Paranoid (IV) Inmate From: Australia |
posted 08-25-2006 02:35
Ah where are you getting $username from? You're not using globals are you? code: $_SESSION['username]
code: session_save_path('/some_dir_here'); session_start(); header("Cache-control: private"); // print session_id(); // Check for a logout command first if(isset($_POST['userLogout']) && $_POST['userLogout'] == 1) { LogoutUser(); } // Check for the sessions vars if(isset($_SESSION['user_login']) && isset($_SESSION['str_random'])) { // Verify username etc } else { // Denied }
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 08-25-2006 10:09
I suggest not to set session_save_path. That's a server side setting that you should not have to change in your scripts. |
Obsessive-Compulsive (I) Inmate From: |
posted 08-25-2006 10:25
oh sorry yea... let me copy it now that i'm on my laptop... oh the thing is if i put it in the edit.php it'll ask me to login again. code: <? session_start(); if(!isset($username) | !isset($password)) { $fail = 0; login($fail); exit(); } $_SESSION['username']; $_SESSION['password']; $result = mysql_query ("SELECT password, name, user, level FROM user WHERE username = '$username'", $connect); $row = mysql_fetch_row($result); $pass = $row[0]; $name = $row[1]; $user = $row[2]; $level = $row[3]; if(($password == $pass) && ($password != "")) { $valid_user = 1; $_SESSION['name']; $_SESSION['user']; $_SESSION['level']; } else { $valid_user = 0; } mysql_close($connect); //User has logged in $_SESSION[$valid_user]; if (!($valid_user)) { //Clear the variables session_unset(); session_destroy(); $fail = 1; login($fail); exit(); } function login($fail) { ?> <br><br><br> <div id="box-top" style="width:400px" align="left"><div id="box-top-in"><table border="0" cellpadding="0" cellspacing="0" height="20"><tr><td valign="middle"><font class="box-title">Login</font></td></tr></table></div></div> <div id="box-cont" style="width:400px"> <? if ($fail==1) { echo 'Incorrect login username and/or password.'; } ?><br> <form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST"> USERNAME <input type="text" name="username" size="50"><br> PASSWORD <input type="password" name="password" size="50"><br> <input type="submit" value="Login"> </form> <font class="cookies">Please Note: Cookies must be enabled to (1) Login to FE Core and (2) Gain access to the documents and programs inside. Check your internet settings if you don't know how to activate cookies.</font> </div></div> <? } ?>
|
Paranoid (IV) Inmate From: Australia |
posted 08-25-2006 11:05
quote:
|
Obsessive-Compulsive (I) Inmate From: |
posted 08-25-2006 12:05
ok thanks... please do point out programming errors code: <? include("../../../Core.inc"); session_start(); //If the login was done before if (!isset($username) && !isset($password)) { //not logged in $fail = 0; login($fail); exit(); } else { //check authenticity list ($accept, $name, $user, $level) = checkLogin($username, $password, $connect); //if the login fails if (!($accept)) { $fail = 1; login($fail); exit(); } else { //if the login succeeds then reg vars $_SESSION['username']; $_SESSION['password']; //other information $_SESSION['name']; $_SESSION['user']; $_SESSION['level']; } } //Check if they are logged in function checkLogin($username, $password, $connect) { $result = mysql_query ("SELECT password, name, user, level FROM user WHERE username = '$username'", $connect); $row = mysql_fetch_row($result); $pass = $row[0]; $name = $row[1]; $user = $row[2]; $level = $row[3]; if(($password == $pass) && ($password != "")) { $access = TRUE; } else { $access = FALSE; } mysql_close(); return array($access, $name, $user, $level); } //Login Form function login($fail) { ?> <br><br><br> <div id="box-top" style="width:400px" align="left"><div id="box-top-in"><table border="0" cellpadding="0" cellspacing="0" height="20"><tr><td valign="middle"><font class="box-title">Login</font></td></tr></table></div></div> <div id="box-cont" style="width:400px"> <? if ($fail==1) { echo 'Incorrect login username and/or password.'; } ?><br> <form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST"> USERNAME <input type="text" name="username" size="50"><br> PASSWORD <input type="password" name="password" size="50"><br> <input type="submit" value="Login"> </form> <font class="cookies">Please Note: Cookies must be enabled to (1) Login to FE Core and (2) Gain access to the documents and programs inside. Check your internet settings if you don't know how to activate cookies.</font> </div></div> <? } ?>
|
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 08-25-2006 13:12
Ok, this is a bit more rantish than I had aimed at... but there are many issues with this seemingly simple piece of code. |
Paranoid (IV) Inmate From: Australia |
posted 08-26-2006 02:44
As TP Mentioned make sure you escape your variables before using them in your query string: |
Obsessive-Compulsive (I) Inmate From: |
posted 08-26-2006 10:23
wow thanks for so much information and thanks for all the help... i'm gonna try the suggestion in order first to see the results |
Maniac (V) Inmate From: there...no..there..... |
posted 08-26-2006 16:08
quote:
|
Obsessive-Compulsive (I) Inmate From: |
posted 08-28-2006 05:26
thanks cpromt those are good tuts... |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 08-28-2006 09:21
Because you are either never setting $_SESSION['username'] ( using $username = 'something'; instead, which will not get it into the session), |
Obsessive-Compulsive (I) Inmate From: |
posted 08-30-2006 00:43
I've used $_SESSION['username'] = $user; |
Maniac (V) Mad Scientist with Finglongers From: Germany |
posted 08-30-2006 14:53
Please post all of your current code, I don't think I have the time to glassball this one. |
Nervous Wreck (II) Inmate From: |
posted 09-01-2006 10:09
Oki here it is: code: <? include("../../../Core.inc"); session_start(); if (isset($HTTP_POST_VARS['username']) { if (checkLogin($HTTP_POST_VARS['username'], $HTTP_POST_VARS['password'], $connect) { regUser($HTTP_POST_VARS['username']) } } //If the login was done before if (!isset($HTTP_POST_VARS['username']) && !isset($HTTP_POST_VARS['$password'])) { if (!isset($username) && !isset($password)) { //not logged in $fail = FALSE; login($fail); exit(); } else { //check authenticity list ($accept, $name, $user, $level) = checkLogin($username, $password, $connect); //if the login fails if (!($accept)) { $fail = TRUE; login($fail); exit(); } } } else { //check authenticity list ($accept, $name, $user, $level) = checkLogin($HTTP_POST_VARS['username'], $HTTP_POST_VARS['password'], $connect); //if the login fails if (!($accept)) { $fail = TRUE; login($fail); exit(); } else { //if the login succeeds then reg vars $_SESSION['username']; $_SESSION['password']; //other information $_SESSION['name']; $_SESSION['user']; $_SESSION['level']; } } //Check if they are logged in function checkLogin($username, $password, $connect) { $result = mysql_query ("SELECT password, name, user, level FROM user WHERE username = '$username'", $connect); $row = mysql_fetch_row($result); $pass = $row[0]; $name = $row[1]; $user = $row[2]; $level = $row[3]; if(($password == $pass) && ($password != "")) { $access = TRUE; } else { $access = FALSE; } mysql_close(); return array($access, $name, $user, $level); } //Register the User function regUser($user) { $_SESSION['username'] = $user; } //Login Form function login($fail) { ?> <br><br><br> <div id="box-top" style="width:400px" align="left"><div id="box-top-in"><table border="0" cellpadding="0" cellspacing="0" height="20"><tr><td valign="middle"><font class="box-title">Login</font></td></tr></table></div></div> <div id="box-cont" style="width:400px"> <? if ($fail) { echo 'Incorrect login username and/or password.'; } ?><br> <form action="<?=$PHP_SELF?><?if($QUERY_STRING){ echo"?". $QUERY_STRING;}?>" method="POST"> USERNAME <input type="text" name="username" size="50"><br> PASSWORD <input type="password" name="password" size="50"><br> <input type="submit" value="Login"> </form> <font class="cookies">Please Note: Cookies must be enabled to (1) Login to FE Core and (2) Gain access to the documents and programs inside. Check your internet settings if you don't know how to activate cookies.</font> </div></div> <? } ?>
|