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
https://forums.phpfreaks.com/topic/72501-cookie-problem/
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.