PDA

View Full Version : PHP Sessions help...



D-Man22
03-09-2010, 01:50 PM
Okay... so... The sessions are not staying when a user logs in...

here's my "login.php"


<? session_start();
include('database.php');
require "functions.all.php";
if(!isset($_SESSION['loggedin'])){
if(!isset($_POST['loginOK'])){
displayLoginForm();
//Probably gonna call a display class here
}else{


processLogin();


}
}else{ echo "Hello, ".$_SESSION[user].". And welcome to the user system."; }
?>

here is the 2 functions....

function displayLoginForm()
{
echo '<div style="padding-left: 5px;">


<form method="post" name="loginForm" onsubmit="myHabbio.__doLogin();return false;">
<b>Username:</b><br /> <input type="text" name="username" id="username" class="oo" /><br />
<b>Password:</b><br /> <input type="password" name="password" id="password" class="oo" /><br />

<input type="submit" id="loginOK" style="margin-top:3px;" value="Login &raquo;" name="sOK" onclick="myHabbio.__doLogin();return false;" class="oo" /> or FB_connect()<br />
No account? <a href="my1/register.php">Register for free!</a>
</form>
</div>
';


}


//////////////////////


function processLogin() {
$username = $_POST['username'];
$password = sha1($_POST[password]);
$exsists = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username`='" . $username . "'"));
if($exsists == 0){ throwLoginError('Incorrect username/password</b><br />Please try again'); }
else{ $result = mysql_query("SELECT * FROM users
WHERE username='$username'") or die();
$row = mysql_fetch_array( $result );
$password1 = sha1($row[password]);
if($password == $password1){ $_SESSION['user'] = $username; $_SESSION['loggedin'] == 1;
//I have ^^ The sessions set there... but when the user reloads the page, it displays the login form =/
displayUserBox();

}else{ throwLoginError('Incorrect username/password</b><br />Please try again'); }
}


}


When a user logs in; i have it set the sessions and display the "userbox".... but when I refresh the page, the login form is displayed... even though the seesions were already set. HELP :/

Jack!
03-09-2010, 03:21 PM
Looking at the code this is for a retro then? or am i wrong?

D-Man22
03-09-2010, 03:29 PM
No? It's for a fansite if you have to know.

Trigs
03-09-2010, 03:34 PM
Do you have session_start(); in the file with the functions as well? I think you need to global it as well but I'm not sure as I've never tried to set a session within a function.

---------- Post added 03-09-2010 at 11:40 AM ----------

disregard above,

$_SESSION['loggedin'] == 1

should be 1 =

D-Man22
03-09-2010, 03:57 PM
^^Thank you [: that worked (:

Want to hide these adverts? Register an account for free!