Jump to content

Session Login


membot

Recommended Posts

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
}
?>

Link to comment
Share on other sites

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";

 

Link to comment
Share on other sites

Well isn't that what I did? :P

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. :/

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.