Jump to content

can't get values out of a cookie for some reason...


next

Recommended Posts

Playing around with a login form from a book and ran into an issue, can't seem to get what's in my cookies. I have 3 scripts:

secret_page_cookie.php:

<?php
if($_COOKIE['auth'] !== "yes") {
header("Location: login_cookie.php");
exit();
}
?>
<html>
<head>
	<title>secret_page_cookie</title>
</head>
<body>
<p style = "text-align: center; matgin: 0.5in">Hello <?Php $_COOKIE['first_name']; ?><br />Welcome to secret page.</p>
</body>
</html>

login_cookie.php (form processor):

<?php
if (isset($_POST['sent'])) {
	foreach ($_POST as $field => $value) {
		if (empty($value))
			$blank_array[] = $field;
		else
			$good_data[$field] = strip_tags(trim($value));
	}
	if (@sizeof($blank_array) > 0) {
		$message = "<ul style = 'color: red; font-weight: bold;'>";
		foreach($blank_array as $error_list)
			$message .= "<li>$error_list cannot be blank</li>";
		$message .= "</ul>";

	//extract($blank_array);
	//extract($good_data);
	include("form_log.php");
	exit();
	}
	$connection = @mysql_connect("localhost", $_POST['user_name'], $_POST['user_password']);
	if(mysql_error()) {
		$message = "<p style = 'color: red; font-weight: bold;'>Invalid username or password</p>";
		include("form_log.php");
		exit();
	}
	mysql_select_db("practice_db");
	$query = "SELECT name FROM members WHERE name = '$_POST[user_name]' and password = '$_POST[user_password]'";
	$result = mysql_query($query);
	$row = mysql_fetch_assoc($result);
	setcookie("first_name", $row['name']);
	setcookie("auth", "yes");
	header("Location: secret_page_cookie.php");
}
else {
	$user_name = "";
	$password = "";
	include("form_log.php");
}
?>

form_log.php

<html>
<head>
	<title>form_log</title>
</head>
<body>
<?Php
if (isset($message))
	echo $message;
?>

<form action = 'login_cookie.php' method = 'post' style = 'margin: .5in'>
		<p><label for = 'user_name' style = 'font-weight: bold; padding-bottom: 1em;'>User ID:</label>
		<input type = 'text' name = 'user_name' id = 'user_name' /></p>

		<p><label for = 'password' style = 'font-weight: bold;'>Password:</label>
		<input type = 'password' name = 'user_password' id = 'user_password' /></p>

		<p><input type = 'submit' value = 'Log in' /></p>
		<input type = 'hidden' name = 'sent' value = 'yes' />
	</form>
</body>
</html>

 

So i set up a user in my test DB, i successfully logged in, but when i get to secret_page_cookie.php it's not displaying my name, why???

I checked cookie value using javascript ( javascript:alert(document.cookie) )and everything seems to be fine, i see my variable.

 

How do  fix this?

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.