Twist3d Posted March 12, 2010 Share Posted March 12, 2010 Hello, well i'm trying to create a simple Admin/Moderator login. But it oddly enough doesn't work. And i'm usually blind and can't spot obvious things so thats what I think it is. But can anyone help? <?php session_start(); if (isset($_SESSION['Mod'])){ echo "Mod"; }else{ if (isset($_SESSION['Admin'])){ echo "Admin"; }else{ echo "Login"; } } ?> This is just the test, but this is on a Navigation bar, this is supposed to changed it to what session is currently logged. So if I use this PHP code to login: case 'login2': session_start(); $password = $_POST['password']; $sql = "SELECT * FROM passwords WHERE Admin='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ session_register("Admin"); echo "<center>Logged in as Administrator</center>"; }else{ $sql2="SELECT * FROM passwords WHERE Moderator='$password'"; $result2 = mysql_query($sql2); $count2 = mysql_num_rows($result2); if($count2==1){ session_register("Mod"); echo "<center>Logged in as Moderator</center>"; }else{ echo "<center>Wrong password!</center>"; } } break; Then this should log me in as a Mod or Admin, which will changed the Button on the Navbar to either Mod, Admin or Login depending on what session i'm logged in as. But this doesn't work. I type in the password on the form page, it says on the login2 page (The code above) that i'm logged in as either an admin or mod, but on the page where the NavBar button is supposed to change it remains always "Login". Any help? Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/ Share on other sites More sharing options...
herghost Posted March 12, 2010 Share Posted March 12, 2010 session_register is no longer supported, you should use $_session['session_name']; Try that and see what happens Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025081 Share on other sites More sharing options...
Twist3d Posted March 12, 2010 Author Share Posted March 12, 2010 So the new code is now: <?php session_start(); if (isset($_SESSION['Mod'])){ echo "Pie"; }else{ if (isset($_SESSION['Admin'])){ echo "Banana"; }else{ echo "Login"; } } ?> case 'login2': session_start(); $password = $_POST['password']; $sql = "SELECT * FROM passwords WHERE Admin='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ $_session['Admin']; echo "<center>Logged in as Administrator</center>"; }else{ $sql2="SELECT * FROM passwords WHERE Moderator='$password'"; $result2 = mysql_query($sql2); $count2 = mysql_num_rows($result2); if($count2==1){ $_session['Mod']; echo "<center>Logged in as Moderator</center>"; }else{ echo "<center>Wrong password!</center>"; } } break; Still doesn't work. Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025082 Share on other sites More sharing options...
herghost Posted March 12, 2010 Share Posted March 12, 2010 Try This: case 'login2': session_start(); $password = $_POST['password']; $sql = "SELECT * FROM passwords WHERE Admin='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ $_session['Admin'] = 'admin'; echo "<center>Logged in as Administrator</center>"; }else{ $sql2="SELECT * FROM passwords WHERE Moderator='$password'"; $result2 = mysql_query($sql2); $count2 = mysql_num_rows($result2); if($count2==1){ $_session['Mod'] = 'mod'; echo "<center>Logged in as Moderator</center>"; }else{ echo "<center>Wrong password!</center>"; } } break; I take it your page is refreshing after the query is made? If you are using an ajax link then your navbar will not refresh unless you have it set too, and so the navbar will not recheck for the session Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025084 Share on other sites More sharing options...
Twist3d Posted March 12, 2010 Author Share Posted March 12, 2010 Try This: case 'login2': session_start(); $password = $_POST['password']; $sql = "SELECT * FROM passwords WHERE Admin='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ $_session['Admin'] = 'admin'; echo "<center>Logged in as Administrator</center>"; }else{ $sql2="SELECT * FROM passwords WHERE Moderator='$password'"; $result2 = mysql_query($sql2); $count2 = mysql_num_rows($result2); if($count2==1){ $_session['Mod'] = 'mod'; echo "<center>Logged in as Moderator</center>"; }else{ echo "<center>Wrong password!</center>"; } } break; I take it your page is refreshing after the query is made? If you are using an ajax link then your navbar will not refresh unless you have it set too, and so the navbar will not recheck for the session Nope, still doesn't work. But I don't get exactly what you mean by the refreshing bit. I don't have any ajax code in my project. And nothing refreshes, the code above is the login and the navbar feature. Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025085 Share on other sites More sharing options...
herghost Posted March 12, 2010 Share Posted March 12, 2010 Ok, imagine it like this: Page loads, reads from top to bottom You login - Does page redirect after this? Does your redirect reload your navbar? Basically your navbar has already been processed, the code will not be read again unless the navbar is refreshed, so it is basically seeing on 1st run isset(sessioninfo) = no and continuing, to get it to read yes, it will have to run again, does that make sense? Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025088 Share on other sites More sharing options...
Twist3d Posted March 12, 2010 Author Share Posted March 12, 2010 No, after you login it doesn't redirect. It just stays on the page "Logged in as Mod/Admin". So fare i don't think anything redirects. So after the user logs in, I will make it redirect to the home page? Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025089 Share on other sites More sharing options...
herghost Posted March 12, 2010 Share Posted March 12, 2010 Yup: something like <?php Header(Location: 'http://www.mysite.com/index.php'); ?> Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025090 Share on other sites More sharing options...
Twist3d Posted March 12, 2010 Author Share Posted March 12, 2010 I tried your way and this way: case 'login2': session_start(); $password = $_POST['password']; $sql = "SELECT * FROM passwords WHERE Admin='$password'"; $result = mysql_query($sql); $count = mysql_num_rows($result); if($count==1){ $_session['Admin'] = 'Admin'; echo "<center>Logged in as Administrator</center>"; ?> <meta http-equiv="REFRESH" content="0;url=index.php"></HEAD> <?php }else{ $sql2="SELECT * FROM passwords WHERE Moderator='$password'"; $result2 = mysql_query($sql2); $count2 = mysql_num_rows($result2); if($count2==1){ $_session['Mod'] = 'Mod'; echo "<center>Logged in as Moderator</center>"; ?> <meta http-equiv="REFRESH" content="0;url=index.php"></HEAD> <?php }else{ echo "<center>Wrong password!</center>"; } } break; Both redirected, neither worked. Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025093 Share on other sites More sharing options...
Tazerenix Posted March 12, 2010 Share Posted March 12, 2010 the superglobal variable is $_SESSION not $_session. As far as i know variable names are case sensitive. Thats most likely your problem Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025095 Share on other sites More sharing options...
Twist3d Posted March 12, 2010 Author Share Posted March 12, 2010 the superglobal variable is $_SESSION not $_session. As far as i know variable names are case sensitive. Thats most likely your problem Thanks! That is what it was! Also thanks to herghost for helping! Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025099 Share on other sites More sharing options...
Tazerenix Posted March 12, 2010 Share Posted March 12, 2010 no problemo Link to comment https://forums.phpfreaks.com/topic/194981-php-login-problem/#findComment-1025100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.