Cookies

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
James138
Forum Newbie
Posts: 18
Joined: Thu Feb 05, 2004 1:41 pm

Cookies

Post 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
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Code: Select all

setcookie();

// or GET the cookie ?
me didnt understand
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post 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?
James138
Forum Newbie
Posts: 18
Joined: Thu Feb 05, 2004 1:41 pm

Post by James138 »

how do u set a timeline for the cookie? thanks!
sandrol76
Forum Newbie
Posts: 8
Joined: Mon Mar 01, 2004 3:25 am

Post 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!
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

a better example would be to refer to the manual.

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