Jump to content

[SOLVED] Cookies


sstangle73

Recommended Posts

There is probably a problem with your script as I get no cookies set when I fill in your form and submit it even though my browser does except cookies.

 

Post the the code here that sets the cookie and how you retrieve the cookie.

 

Make sure when you set the cookie there is now output before you use setcookie function. To retrieve a cookie you must use $_COOKIE['cookie_name_here'] variable.

Link to comment
Share on other sites

To get the data

<?
if (isset($_COOKIE['UserName'])) {
echo
?>	
<form action="mail.php" method="post">
Your Name: <input type="text" name="name"><br>
E-mail: <input type="text" name = "email"><br><br>
Comments<br>
<textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>
<?
} else {
echo
?>					
<form action="name.php" method="post">
Your Name: <input type="text" name="UserName"><br>
<input type="submit" value="Remember My Name!">
</form>
<?
}
?>

 

on name.php:

 

<?php
setcookie("UserName", $_POST['UserName'], time()+604800);
?>
<html>...</head>...
<?php
$UserName=$_POST['UserName'];
php?>
<?php
echo "Thanks $UserName";
php?>
<br>
<?php
if (isset($_COOKIE["user"]))
{
echo "Your name will be remembered for one week or untill your cookies are cleared!";
}
else
{
echo "Error Cookie not set Please try again.  Check to make sure you browser allows cookies!";
}
php?>

 

 

here is retriveing the cookie back on the index.php:

 

<?php
if (isset($_COOKIE['UserName'])) {
echo "Hello, ".$_COOKIE['UserName']."! Welcome back!";
} else {
echo "Please provide your name below!";
}
php?>

Link to comment
Share on other sites

PHP.net says regarding setcookie():

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

 

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays. Note, superglobals such as $_COOKIE became available in PHP 4.1.0. $HTTP_COOKIE_VARS has existed since PHP 3. Cookie values also exist in $_REQUEST.

 

So your verification in name.php seems pointless.  Also, you should get rid of the empty echo statements in the first batch of code you posted.  If you exit the PHP code, you don't need to use echo statements.  However, this still doesn't correct your problem of them not setting, it's just simple schematics.

Link to comment
Share on other sites

(1)  First check cookies are enable in your browser.

(2)  Try

 

<?php

if(isset($_POST['UserName']) and !empty($_POST['UserName'])){

 

setcookie("UserName", $_POST['UserName'], time()+604800, "/");

 

}else{

 

echo "Problem";

 

}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.