Jump to content

Cookie Problem


The Little Guy

Recommended Posts

OK, I have a form, for a login, if it finds that the username and password match, it saves some info in a session, then creates a cookie with the user's username in it.

 

my problem is that in order to be redirected to users.php, I need to login 2 times.

to see what I mean, go here: http://tzfiles.com

User Name: demo

Password: demo12

 

If an incorrect password is entered, no cookie is saved, if a correct password is entered it saves a cookie and asks you to login again.

 

<?php include'db.php';
if(isset($_POST['login'])){
if(isset($_COOKIE['userName'])){
	$user = $_COOKIE['userName'];
}
if(isset($_POST['user'])){
	$user = $_POST['user'];
}
$sql = mysql_query("SELECT * FROM users WHERE user='".addslashes($user)."' AND pass='".base64_encode($_POST['pass'])."'")or die(mysql_error());
if(mysql_num_rows($sql) > 0){
	$row = mysql_fetch_array($sql);
	session_start();
	$_SESSION['user'] = $row['user'];
	$_SESSION['first'] = $row['fname'];
	$_SESSION['last'] = $row['lname'];
	$_SESSION['id'] = $row['id'];
	$_SESSION['email'] = $row['email'];
	$_SESSION['sort_by'] = $row['sort_by'];
	$_SESSION['fpp'] = $row['files_per_page'];
	$_SESSION['group'] = $row['group'];
	$_SESSION['logged'] = 1;
	setcookie("userName", $row['user'], time()+60*60*24*7);
	header("Location: ../user.php");
	exit;
}
}
?>

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.