Page 1 of 1

Cookies

Posted: Fri Mar 05, 2004 11:51 am
by James138
Hi,
Ive created a cookie on my login page which apparently works since i use Javascript:alert(document.cookie); and it picks up the cookie. But when i try to pass the cookie on any other page its not working. Any ideas?
heres my code

Code: Select all

<?php 
require_once('../Connections/james.php'); 
function output_form ($error_message = '',$username ='')
{
print
'<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<div align="center"><p><img src="../Templates/background1_r1_c1.gif" width="800" height="151"></p>
  <p>Welcome to Golf Competitions Online. </p>
  <form method="POST" action"http://localhost/project/memberoptions/membersoptions.php">
	<p></p>';
  if ($error_message !='')
{
print '
 <font color=Green>' .$error_message. '</font>';
}
print '
<p>Please Enter in your username and password Below:</p>
<br>
<p>User Name: <input text type="text" name="username"';
	if ($username !='')
	{
	print 'value= "'.$username.'"';
	}
	print'  
    </p>
    <p>Password: <input name="Password" text type="password">
    </p>
    <p> 
      <input type="submit" name="Submit" value="Submit">
      <input type="reset" name="Submit2" value="Reset">
    </p>
</form>

  <p align="left">Administrator enter here. </p>
  <p align="left"><a href="http://localhost/project/Login Page/AdministratorLogin.php">enter details page</a><br>
  </p>
  <p>&nbsp;</p>
</div>
</body>
</html>';
}
mysql_select_db($database_james, $james);
if (isset($_POST['username']) and isset($_POST['Password']))
{
$sql = "SELECT password, memberID FROM membertable WHERE memberID ='$username' AND password = '$Password'";
   
   $result = mysql_query($sql) or die("Error: Mysql error".mysql_error());
 
 	 $num = mysql_num_rows($result); 
	 $row = mysql_fetch_assoc($result);
 
 	if ($num != 0)
	{
	setcookie("user_name", $_POST['username']);
header ("location: http://localhost/project/memberoptions/ ... ptions.php");
}
else
{
output_form ('Incorrect please try again');
}
}
else
{
output_form();
}
?>
Edit: added php highlighting - Sami

Posted: Fri Mar 05, 2004 2:48 pm
by ol4pr0

Code: Select all

setcookie();

// or GET the cookie ?
me didnt understand

Posted: Fri Mar 05, 2004 5:43 pm
by tim
if you set a cookie and not assign a time value for it, it will delete itself after the browser is closed...

can that be the culprit?

Posted: Sat Mar 06, 2004 12:12 pm
by James138
how do u set a timeline for the cookie? thanks!

Posted: Sat Mar 06, 2004 12:32 pm
by sandrol76
Yes you have to set the expire time of the cookie.
You set the expire time this way:
setcookie("CookieName", $value, time()+$num_of_seconds);
where $value is the value you want to assign to the cookie and $num_of_seconds is the number of seconds after which you want your cookie to expire!
Bye from Italy!

Posted: Sat Mar 06, 2004 3:45 pm
by tim
a better example would be to refer to the manual.

http://www.php.net/manual/en/function.setcookie.php