Jump to content


Photo

$_SESSION ERROR


  • Please log in to reply
1 reply to this topic

#1 Drezard

Drezard
  • Members
  • PipPipPip
  • Advanced Member
  • 244 posts

Posted 17 September 2006 - 05:44 AM

It keeps giving me this output:

"Cookie not created"


Heres login_form.php:
<?php
// initialize a session
session_start();
?>
<html>
<head></head>
<body>

<?php
if (!isset($_SESSION['userinfo']) && !isset($_COOKIE['user']) && !isset($_POST['pass'])) {
    // if no data, print the form
?>
    <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
       Username:<input type="text" name="user"><br>
	   Password:<input type="password" name="pass"><br>
        <input type="submit" name="submit">
    </form>
<?php
}
if (!isset($_SESSION['userinfo']) && !isset($_COOKIE['user']) && isset($_POST['pass'])) {

	include('connect.php');
    // if a session does not exist but the form has been submitted
    // check to see if the form has all required values
    // create a new session
	$user = empty($_POST['user']) ? die ("Please Enter A Username") : mysql_escape_string($_POST['user']);
	$pass = empty($_POST['pass']) ? die ("Please Enter A Password") : mysql_escape_string($_POST['pass']);
	$sql = "SELECT * FROM users WHERE user='$user' AND pass='$pass'";
	$result = mysql_query($sql);
	$count=mysql_num_rows($result);
	
	if($count==1){
		
		$_SESSION['userinfo'] = $user;
		?>
		
		<meta http-equiv="refresh" content="0;url=login_sucess.php">;

		<?php	
	}
		
	if ($count == 0) {
		
		echo "Username or password are incorrect";

		}	
	
}
if (isset($_COOKIE['user'])) {
echo "You are already logged in";
}
?>
</body>
</html>

Heres login_sucess.php:

<?php
session_start();
//if (isset($_SESSION['userinfo']{
//$pass = $_SESSION['pass'];
//setcookie('pass', $pass, time()+36000*24*365);
$user = $_SESSION['userinfo'];
setcookie('user', $user, time()+36000*24*365);
session_destroy();
//}
?>
<htmL>

<head>

</head>

<body>

<?php

if (isset($_COOKIE['user'])) {
echo "Login Complete, Welcome $user";
}

if (!isset($_SESSION['userinfo'])) {
?>
		
<meta http-equiv="refresh" content="0;url=login_form.php">;

<?php	
}

if (!isset($_COOKIE['user'])) {
echo "Cookie not created";
}
?>

</body>
</html>



Thanks, Daniel

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 September 2006 - 05:47 AM

This is basically the same 'problem' you have already detailed in another thread. Please do not double post.

http://ca.php.net/ma...n.setcookie.php

Read the common pitfalls section of that page, especially the very first one. You'll have a much better understanding of cookies after reading those pitfalls.
Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users