Menu
Is it possible to create a cookie when a link is clicked?
and for the cookie to be called a certain name, and then for that name to show up on the next page?
[code=php]
<?php
// no HTML output until after we set the cookie
setcookie('ServerName', 'Server1', time()+60*60*24*3);
// now that we set the cookie, we can print its output
echo $_COOKIE['ServerName']; // prints "Server1"
?>
[/code]
[code=php]
<a href="http://www.domain.ext/thepage.php?form=television">fill my form</a>
[/code]
[code=php]
<form>
<input type="text" value="<?php echo($_GET['form']); ?>">//resulting with "television"
</form>
[/code]
[font=trebuchet ms]You’re currently at: home.php. You link to: page.php. This is the code in page.php:[/font]
[code=php]
<?php
// no HTML output until after we set the cookie
setcookie('ServerName', 'Server1', time()+60*60*24*3);
// now that we set the cookie, we can print its output
echo $_COOKIE['ServerName']; // prints "Server1"
?>
[/code]
[font=trebuchet ms]In the PHP.net specification (and most other specs), parameters in the [brackets] are optional. Hence, for the setcookie function, the only required parameter is the name of the cookie.[/font][/QUOTE]
<i>
</i><p><a href="cookie.php?link=showcookie.php&value=cookie+value">
Link text</a></p>
[code=php]
<?php
if(!empty($_GET['link']) and !empty($_GET['value']))
{
setcookie("cookie", $_GET['value'], time() + 60*60*24*365); // 1 year
header("Location: {$_GET['link']}");
exit;
}
elseif(!empty($_SERVER['HTTP_REFERER']))
{
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
}
else // we're screwed, do redirect to home page
{
header("Location: http://www.charles-reace.com/");
exit;
}
?>
[/code]
[code=php]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=ISO-8859-1'>
<title>Show Cookie</title>
</head>
<body>
<h1>Show Cookie Page</h1>
<?php
if(isset($_COOKIE['cookie']))
{
echo "<p>Cookie value = {$_COOKIE['cookie']}</p>n";
}
?>
</body>
</html>
[/code]
0.1.9 — BETA 6.17