...

View Full Version : Php Cookies Help



gudipudi
05-09-2009, 11:06 AM
Hello Friends,

my doubt is ...

when a person clicks on a link present on Page A, it should check for the cookies (if returning or new visitor) and should take him to 2 separate pages.


i.e Page A to Page B if returning visitor
Page A to Page C if new visitor.

i having started learning php and if any one could provide me the working code, it will help me a lot.

thanks in advance

Zangeel
05-09-2009, 11:18 AM
the page for the link use this



<?php


if (isset($_COOKIE['history'])) {

$link = "http://link.com/c.html";

} else {

$link = "http://link.com/b.html";
}

echo "<a href=\"$link\">Link</a>";




?>


on the page you want to make the cookie use something like


setcookie("history", "I've been here", time()+9999);

gudipudi
05-09-2009, 11:22 AM
thanks for the quick reply sir,

i am trying it now and update you in 5min

gudipudi
05-09-2009, 11:26 AM
I am getting this error zangeel ( plz check this page http://tycheit.com/rama.php )

thesavior
05-09-2009, 11:39 AM
It means you already output contents to the page. You have to set the cookie before anything else in the script.

gudipudi
05-09-2009, 11:49 AM
It means you already output contents to the page. You have to set the cookie before anything else in the script.

below is the code how i placed in the page


<html>
<head></head>
<body>
<?php
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
?>
<?php
if (isset($_COOKIE['lastVisit'])) {

$link = "c.html";
} else {
$link = "b.html";
}
echo "<a href=\"$link\">Link</a>";
?>
</body>
</html>

abduraooft
05-09-2009, 12:45 PM
Try something like


<?php
if(isset($_COOKIE['lastVisit'])){
$link = "c.html";
}
else{
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
$link = "b.html";
}
?>
<html>
<head></head>
<body>

<?php

echo "<a href=\"$link\">Link</a>";
?>
</body>
</html>

gudipudi
05-09-2009, 01:03 PM
Try something like


<?php
if(isset($_COOKIE['lastVisit'])){
$link = "c.html";
}
else{
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie('lastVisit', date("G:i - m/d/y"), $inTwoMonths);
$link = "b.html";
}
?>
<html>
<head></head>
<body>

<?php

echo "<a href=\"$link\">Link</a>";
?>
</body>
</html>

Thanks sir, the previous error is gone....but it still opens link C even if refresh cookies or delete cookies

abduraooft
05-09-2009, 01:05 PM
Could you please update your online page ?

gudipudi
05-09-2009, 01:22 PM
Could you please update your online page ?

updated and still the same
http://tycheit.com/rama.php

abduraooft
05-09-2009, 01:28 PM
updated and still the same
http://tycheit.com/rama.php
It's working as specified in the code. For the first time the link points to b.html and when I refresh the page, it's getting pointed to c.html
Do you need to check whether the user visited that page? Then you need to set the cookie from that page (You may need to change the extension to .php)

gudipudi
05-09-2009, 01:35 PM
It's working as specified in the code. For the first time the link points to b.html and when I refresh the page, it's getting pointed to c.html
SO you need to check whether the user visited that page? Then you need to set the cookie from that page (You may need to change the extension to .php)

thanks a lot sir for your kind help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum