Jump to content

Unable to login using sessions


Pain

Recommended Posts

Hello.

 

I am coding a remember me feature. Everything is working, except i am being logged in using cookies even when i want to use a session.

 

To login using a cookie i must select the checkbox, for sessions i must leave it blank.

 

Here is my code, if someone could spot a mistake i would be really grateful.

 

Login page


<?php

ob_start();

// starting session...
session_start();

// requiring connection...
require("functions.php");



// assigning variables...
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$submit = mysql_real_escape_string($_POST['submit']);
$rememberme = $_POST['rememberme'];

// querying database...
$query = mysql_query("SELECT * FROM users WHERE username = '$username'");
$numrows = mysql_num_rows($query);

if ($numrows != 0)
{
while ($row = mysql_fetch_assoc($query))
{
	$db_username = $row['username'];
	$db_password = $row['password'];
}
}

// verifying login details...
if ($submit)
{
if (!empty($username) && !empty($password))
{
	if ($username == $db_username && $password == $db_password)
	{
		if ($rememberme = "on")
		{
			setcookie("username", $username, time() + 7200);
	        header('Location: tablets.php');
		}
		else
		{
	        $_SESSION['username'] = $db_username;
	        $url = $_SESSION['origin'] ? $_SESSION['origin'] : "main.php"; 
            unset($_SESSION['origin']);
            header('Location: ' . $url);
            exit;
		}		
	}
	else
	{
		echo "Incorrect login details";
	}
}
else
{
	echo "You must type in username and password";
}
}


ob_end_flush();

?>

 

Login form

<?php

session_start();

require("connect.php");

if (isset($_SESSION['username']) || isset($_COOKIE['username']))
{
header('Location: main.php');
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Login</title>

<link rel="stylesheet" type="text/css" href="form.css" />

</head>
<body>




<form method="post" action="login.php">
<div class="box"> 
<h1>Login</h1>
<label>
<span>Username</span>
<input type="text" class="input_text" name="username" id="name" />  
</label>
<label>
<span>Password</span>
<input type="password" class="input_text" name="password" id="password" /> 
</label>
<input type="checkbox" name="rememberme" />
<label>
<input type="submit" class="button" value="Login" name="submit" /> 
</label>
</div>
</form>




</body>
</html>

Link to comment
Share on other sites

I think you missed the point. You were using $rememberme = "on" with one equal sign, which is an assignment operator - instead of two equal signs which is the comparison operator.

 

Besides the usual way to identify if a checkbox was checked is simply isset(). In many cases (such as this one) the value of the checkbox really is not important.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.