Guest Null Posted March 1, 2010 Posted March 1, 2010 Ok so im really bad with sessions and i need help. Heres the code. Problem: Sessions arent passing user data correctly index.php <?php session_start(); if(get_magic_quotes_gpc() == 0) { foreach($_POST as $k => $v) { $_POST[$k]=addslashes($v); } foreach($_GET as $k => $v) { $_GET[$k]=addslashes($v); } } switch($_GET['do']){ case 'auth': auth(); break; default: index(); break; } function index(){ print '<table width="100%" cellspacing="1" cellpadding="1" style="text-align:center;"><tr><th>Login to Hardcore Mafia</th></tr> <form action="index.php?do=auth" method="post"> <tr><td> Username:<input type="text" name="username"></td></tr> <tr><td> Password:<input type="password" name="password"></td></tr> <tr><td> <input type="submit" value="Login!"></td></tr> </form> </table>'; } function auth(){ $host="*Blanked out*"; $user="*Blanked out*"; $pass="*Blanked out*"; $database="*blanked out*"; $cid = mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($database,$cid) or die(mysql_error()); if($_POST['username']=="" || $_POST['password']==""){ die("Please Fill in All Fields"); } $userdata = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' AND password='{$_POST['password']}'") or die(mysql_error()); if(mysql_num_rows($uq)==0){ die("Invalid Login!"); } else{ $_SESSION['loggedin']=1; $fetch = mysql_fetch_row($uq); header("Location: loggedin.php"); } } ?> loggedin.php <?php session_start(); $host="dkdkdt"; $user="ejkmf"; $pass="ejfkdf"; $database="ejkdksdkjsdi983838"; $cid = mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($database,$cid) or die(mysql_error()); $userid=$_SESSION['userid']; $uquery = mysql_query("SELECT * FROM users WHERE userid={$userid}",$cid); $udata = mysql_fetch_row($uquery); print "Hey! {$udata['username']}"; ?> When i login, i t doesnt display the username data, and in phpmyadmin, loggedin still equals 0. Quote
Joshua Posted March 1, 2010 Posted March 1, 2010 For one you have nothing defining $ir in loggedin.php no include globals, no db query no nothing :p Quote
Guest Null Posted March 1, 2010 Posted March 1, 2010 oops well its not exactly mccodes but if i post in in the php forum it never gets answered. and i changed $ir to $udata like i have it Quote
Zero-Affect Posted March 1, 2010 Posted March 1, 2010 You didn't define $_SESSION['userid'] in the authenticate (just scanned over it quickly) so maybe afew edits could be editing line 48 $userdata into $uq and below the fetch_row (line 56) maybe add $_SESSION['userid'] = $fetch['userid']; NOTE: this is index.php Quote
Joshua Posted March 1, 2010 Posted March 1, 2010 just told him that on msn :p didnt look at that script first >< Quote
Guest Null Posted March 2, 2010 Posted March 2, 2010 Well, ive tried that too crimgame, it just doesnt seem to want to pass the sessions through correctly. i fixed all the definitions and added that thing you asked me too, and it still wont work :/ Quote
Djkanna Posted March 2, 2010 Posted March 2, 2010 <?php session_start(); if(get_magic_quotes_gpc() == 0) { foreach($_POST as $k => $v) { $_POST[$k]=addslashes($v); } foreach($_GET as $k => $v) { $_GET[$k]=addslashes($v); } } switch($_GET['do']){ case 'auth': auth(); break; default: index(); break; } function index(){ print '<table width="100%" cellspacing="1" cellpadding="1" style="text-align:center;"><tr><th>Login to Hardcore Mafia</th></tr> <form action="index.php?do=auth" method="post"> <tr><td> Username:<input type="text" name="username"></td></tr> <tr><td> Password:<input type="password" name="password"></td></tr> <tr><td> <input type="submit" value="Login!"></td></tr> </form> </table>'; } function auth(){ $host="*Blanked out*"; $user="*Blanked out*"; $pass="*Blanked out*"; $database="*blanked out*"; $cid = mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($database,$cid) or die(mysql_error()); if($_POST['username']=="" || $_POST['password']==""){ die("Please Fill in All Fields"); } $userdata = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' AND password='{$_POST['password']}'") or die(mysql_error()); if(mysql_num_rows($userdata)==0){ die("Invalid Login!"); } else{ $_SESSION['loggedin']=1; $fetch = mysql_fetch_array($userdata); $_SESSION['userid'] = $fetch['userid']; header("Location: loggedin.php"); } } ?> Try that. Quote
Djkanna Posted March 3, 2010 Posted March 3, 2010 No problem, It had been said previously ;) Reek13: First time ever :O Quote
Guest Null Posted March 3, 2010 Posted March 3, 2010 hey ok it was working, for some reason when i logout its not letting me login again. and it wont let 2 users on at the same time Quote
Guest Null Posted March 3, 2010 Posted March 3, 2010 Okay, well i made a simple check(php file), to check the sessions. <?php session_start(); if($_SESSION['loggedin']) { $check1 = "Yeap"; } else { $check1="Nope"; } if($_SESSION['mobid']) { $check2 = $_SESSION['mobid']; } else { $check2 = "Invalid!"; $_SESSION['mobid']=1; } print "Logged In: {$check1} Mob Id:{$check2}"; ?> Basically that will tell me which ones are working. When i go there without doing login.php first, it says nope to everything, which is good. But when i do login and go there, it returns Logged In: Yeap Mob ID: Invalid. I have it so it sets my mob id to 1 if it isnt set, but thats just so i can get it under ID 1. So basically my logged in is being set but my mob id isnt Quote
Djkanna Posted March 3, 2010 Posted March 3, 2010 Have you changed the file (index.php) to use $_SESSION['mobid'] rather than $_SESSION['userid']? Quote
Djkanna Posted March 3, 2010 Posted March 3, 2010 Try this: <?php session_start(); if(get_magic_quotes_gpc() == 0) { foreach($_POST as $k => $v) { $_POST[$k]=addslashes($v); } foreach($_GET as $k => $v) { $_GET[$k]=addslashes($v); } } switch($_GET['do']){ case 'auth': auth(); break; default: index(); break; } function index(){ print '<table width="100%" cellspacing="1" cellpadding="1" style="text-align:center;"><tr><th>Login to Hardcore Mafia</th></tr> <form action="index.php?do=auth" method="post"> <tr><td> Username:<input type="text" name="username"></td></tr> <tr><td> Password:<input type="password" name="password"></td></tr> <tr><td> <input type="submit" value="Login!"></td></tr> </form> </table>'; } function auth(){ $host="*Blanked out*"; $user="*Blanked out*"; $pass="*Blanked out*"; $database="*blanked out*"; $cid = mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($database,$cid) or die(mysql_error()); if($_POST['username']=="" || $_POST['password']==""){ die("Please Fill in All Fields"); } $userdata = mysql_query("SELECT * FROM users WHERE username='{$_POST['username']}' AND password='{$_POST['password']}'") or die(mysql_error()); if(mysql_num_rows($userdata)==0){ die("Invalid Login!"); } else{ $_SESSION['loggedin']=1; $fetch = mysql_fetch_array($userdata); $_SESSION['mobid'] = $fetch['userid']; header("Location: loggedin.php"); } } ?> Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.