/    Sign up×
Community /Pin to ProfileBookmark

Is this possible in PHP?

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?

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@chazzyFeb 12.2006 — to clarify, the cookie doesn't get set until after the next page has finished processing. Typicalliy, I'd recommend something like a self-processing page with PHP redirects to move on to the next page.
Copy linkTweet thisAlerts:
@BervanauthorFeb 12.2006 — ok so i need maybe a crash course code...

i didnt see a specific code on that site set up as an example for me to use.

so i need to set up a cookie named "ServerName"and its value is lets say "Server1" and for it to expire after 3 days.

so i dont know exactly how to set that up... i saw on that site alot more information that was in the setcookie()... can it be left out?

how would i put the cookie in a <a href> tag?

i have a redirect site that i can use as a pre site for the sookie to actually be set up

and then how would i be able to use the cookie on the following page after the redirect page.

What i have is a list of server names. it would be convinent to be able to put the server name at as a header up at the top of the 2nd page. because all of the server sites would be the same layout... it would be easier to just set up 50+ cookies instead of creating 50+ html's.
Copy linkTweet thisAlerts:
@BervanauthorFeb 12.2006 — Would i have an easier time using a different type of program?... im not restricted to PHP or a cookie for that matter... i just to to gather data from clicking a link and send that data into a text from on the next page as a title or header
Copy linkTweet thisAlerts:
@JonaFeb 12.2006 — [font=trebuchet ms]You&#8217;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]
Copy linkTweet thisAlerts:
@JonaFeb 12.2006 — [font=trebuchet ms]No, PHP is perfectly capable and rather easy when dealing with things like this. This is what PHP was built for.[/font]
Copy linkTweet thisAlerts:
@SheldonFeb 12.2006 — So you want the text from a link to populate a text box?

apage.php

[code=php]
<a href="http://www.domain.ext/thepage.php?form=television">fill my form</a>
[/code]

and thepage.php
[code=php]
<form>
<input type="text" value="<?php echo($_GET['form']); ?>">//resulting with "television"
</form>
[/code]


Buth then how are you creating the link?
Copy linkTweet thisAlerts:
@chazzyFeb 12.2006 — [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]


Actually, Jona, that was the point I was trying to make - something that was as literally as what you just posted will not work, as cookie writing only occurs after the page has been sent to the browser. You can read the cookie on the next page after its been set on the first page, but you can't set and read a cookie on the same page.

Also, the actual setting of the cookie can't occur "on click" like you had requested. That's just the nature of what PHP is - a server side language. Javascript could set a cookie on click, then PHP could read it on the next page. You can't put the cookie in a "<a>" tag, that's not what it does. Are you trying to grab what link the user clicks, and verify that that's where they are afterwards? Another alternative would be to use a session variable - set it to where they're supposed to be, when they're not there, throw an error.
Copy linkTweet thisAlerts:
@NogDogFeb 12.2006 — Link:
<i>
</i>&lt;p&gt;&lt;a href="cookie.php?link=showcookie.php&amp;value=cookie+value"&gt;
Link text&lt;/a&gt;&lt;/p&gt;

cookie.php:
[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]

showcookie.php:
[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]
Copy linkTweet thisAlerts:
@BervanauthorFeb 12.2006 — well i threw that all together in the appserv www folder... it the cookie.php didnt link to either showcookie or to that other website.. it just sat there.maybe instead of trying to auto link it just put a link on the cookie.php that links to showcookie.php. i can deal with html with ease... and simple simple PHP...i cant write php yet but i can edit already written PHP so im trying to get as much as i can to mess around with. so... yea can i have more help ?
Copy linkTweet thisAlerts:
@BervanauthorFeb 13.2006 — Hello? sry for bringing this up again... but im tryin to get this done and im tryin to get a response(sry to be so rude). Soo anyways that code doesnt work? anyone got some comments as to how to make it work.

ill restate what i want/need--

1 site will have a link on it... in that link i want there to be a name of something

then when the link is clicked it remembers the name of that something. On the next page i want the name of that something to be as a header.

So well have a link <a href="server1.html">Server number 1</a>

and somewhere in there will be the code in an onclick="" or name="" or whatever can be used

so that on the next site it will look like

<html>

<head>

<title>"Server 1" page</title>

</head>

<body>

<h1>"Server 1"</h1>

</body>

<html>


something along the lines of that, should i still be useing php and cookies? thanks for the help everyone already and anyone willing to help a "noob"
Copy linkTweet thisAlerts:
@NogDogFeb 13.2006 — The code I supplied above worked just fine for me when I tried it.
×

Success!

Help @Bervan spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...