membot Posted May 12, 2011 Share Posted May 12, 2011 This should be really simple, but I just can't figure out why it isn't working. It's my first time using sessions, so I'm probably doing something silly. It's just a login to an admin page. It's for a photo gallery, that's why the database is called "photo". This is the login page: <?php session_start(); if(isset($_POST['user']) && isset($_POST['password'])){ $user = $_POST['user']; $password = sha1($_POST['password']); $photo = new mysqli('localhost', 'user', 'password', 'photo'); $login = $photo->query("select user, sha1(password) from settings where user = '$user' and sha1(password) = '$password'"); if($login->num_rows > 0){ $_SESSION['login'] = 1; ?> <META HTTP-EQUIV="Refresh" Content="0; URL=admin.php"> <?php } else { $badlogin = 1; } } ?> <html> <head> <style> body {margin-top: 50px;} td {text-align: right;} input {width: 200px;} </style> </head> <body><center> <?php if(isset($badlogin)){ ?> <span style="color: red;">Oops! Wrong login.</span><br><br> <?php } ?> <table> <form action="admin.php" method="post"> <tr><td>User:</td><td><input type="text" name="user" /></td></tr> <tr><td>Password:</td><td><input type="password" name="password" /></td></tr> <tr><td></td><td><input type="submit" value="Login" /></td></tr> </form> </table> </center></body> </html> And this is the admin page: <?php session_start(); if($_SESSION['login'] != 1){ ?> <META HTTP-EQUIV="Refresh" Content="0; URL=login.php"> <?php } else { ?> <html> <head> </head> <body> Admin stuff here. </body> </html> <?php } ?> Quote Link to comment Share on other sites More sharing options...
jayjay159 Posted May 12, 2011 Share Posted May 12, 2011 Sessions are easy, basically when all the checks are done i.e username and password match those in the database, then set the session $_SESSION['username'] = $username or $_SESSION['login'] = "1"; in your case. if(everthing is good){ $_SESSION['login'] = "1"; }else echo"something went wrong"; Quote Link to comment Share on other sites More sharing options...
membot Posted May 16, 2011 Author Share Posted May 16, 2011 Well isn't that what I did? If the login is correct, it's supposed to set the session variable and redirect to the admin page. And then the admin just checks if the variable is set and if it's not, it redirects to the login page. But when I try it (on WAMP), even after I log in it redirects to the login page again. So my thought was that somehow the session variable wasn't carrying over to the second page. :/ Quote Link to comment Share on other sites More sharing options...
membot Posted May 16, 2011 Author Share Posted May 16, 2011 Oh wow, nevermind. I had the form action set to the wrong page. I am like the queen of stupid mistakes. x3 Quote Link to comment Share on other sites More sharing options...
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.