View Full Version : Resolved If cookie is set, then display content of a different div
KennyP
11-05-2010, 01:16 PM
Hi guys:
Could you please direct me to a script that could set a cookie on the the first visit to a page... then, when it finds that cookie on the second visit, it will display the content of a different div.
I've searched a long time but I cant find anything that will do this. I assume I've been using the wrong search words.
Thanks,
Kenny
Beverleyh
11-05-2010, 03:45 PM
Cookies are really easy to set using jQuery. Here's a link to a few tutorials on Google: http://www.google.co.uk/#hl=en&source=hp&biw=1124&bih=839&q=jquery+cookie+tutorial&aq=2&aqi=g10&aql=&oq=jquery+cookie&gs_rfai=&fp=6750cf575c8019d8
bluewalrus
11-05-2010, 05:07 PM
This should provide you all the information you need for the PHP solution.
http://php.net/manual/en/function.setcookie.php
If you have questions or problems post back.
KennyP
11-05-2010, 08:17 PM
Thank you for your replies, BeverlyH and bluewalrus. I checked both links and found good cookie setting tutorials worth reading. However, they mention nothing in regard to displaying div 1 on the first page visit, and switching to div 2 on all subsequent visits. That is, until the cookie expires.
Will using the php solution always work, as opposed to not working with the java method in browsers where java is disabled?
Thanks
Try:
<?php
if(isset($_COOKIE['div'])){
echo "<div>I can tell you've been here before</div>";
} else {
setcookie('div', true, 5184000 + time()); //hold fo 2 months
echo "<div>Its your first time being here</div>";
}
?>
Good luck!
KennyP
11-05-2010, 09:30 PM
Thank you Nile. I entered your PHP code at the very top of a page and it works perfectly. The proper messages display on first and second visit.
To integrate the actual content of my two divs with your code, do you advise subtituting their content within your code, or should it be done by assigning IDs?
Just change the following:
<?php
if(isset($_COOKIE['div'])){
echo "<div>I can tell you've been here before</div>";
} else {
setcookie('div', true, 5184000 + time()); //hold fo 2 months
echo "<div>Its your first time being here</div>";
}
?>
Red = after cookie is set
Highlighted = first timer
Good luck
KennyP
11-05-2010, 09:57 PM
I moved your code into the body section, as is, but it's now generating "...cannot modify header information...headers already sent."
I came across this problem some time ago with another script, but I can't remember the solution.
EDIT:
I remembered; I removed all white spaces before and after the opening and closing tags, but the same message is still generated.
At the top of your page put:
<?php
$revisit = false;
if(isset($_COOKIE['div'])){
$revisit = true;
} else {
setcookie('div', true, 5184000 + time()); //hold fo 2 months
$revisit = false;
}
?>
Then where you want your divs put:
<?php
if($revisit){
echo "<div>You've been here</div>"
} else {
echo "<div>Its your first time!</div>
}
?>
KennyP
11-05-2010, 10:31 PM
Nile:
It's now working perfectly, after using your previous example to add a quote mark and semicolons, as follows...
<?php
if($revisit){
echo "<div>You've been here</div>";
} else {
echo "<div>Its your first time!</div>";
}
?>
I'll now substitute the contents of my two divs...it should work. :)
No problem, I'm glad to help.
Here on DD, we like to keep things organized. In an effort to do so, you have the option to set a thread to resolved when an issue is fixed. To make the status of the thread resolved:
1. Go to your first post
2. Edit your first post
3. Click "Go Advanced"
4. In the dropdown next to the title, select "RESOLVED"
KennyP
11-06-2010, 12:15 AM
Sorry Nile, I had composed a long reply, but when I clicked submit, I had been logged out. When I logged in again the message was lost.
I'll now recostruct it and will post again soon.
KennyP
11-06-2010, 12:30 AM
Here's the short version...
If I remove all links (not written in PHP) from the text in the second visit div, and I also remove all the div style formatting, it works. Likewise in the first visit div, wherein I need to display a ticker; the non-PHP code generates parsing errors as shown below for the lines of the ticker code...
Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'
} else {
echo "<div style="text-align:justify;" class="style59"> First visit text";
echo "<a href='my_page.php'>First visit linked text!</a>
<br>
<span class="style59" id="anID"
onmouseover="AT_Pause()"
onmouseout="AT_Resume()"></span>
<span><em><script type="text/javascript" src="AttentionTicker/AttentionTickerInclude.php?anID">
</script></em></span>
</div>";
}
?>
Therefore, your code works perfectly, and I managed to get the first visit link to work, but I hit a bump.
If you would you please translate the rest of the non-PHP code to PHP, then the entire action I'm trying to perform would work.
Thanks again Nile
bluewalrus
11-06-2010, 03:54 PM
Don't echo the html if there's multiple lines, it's easier to just close the php then re-open it when finished.
} else {
?>
<div style="text-align:justify;" class="style59"> First visit text";
<a href='my_page.php'>First visit linked text!</a>
<br>
<span class="style59" id="anID" onmouseover="AT_Pause()" onmouseout="AT_Resume()"></span>
<span><em><script type="text/javascript" src="AttentionTicker/AttentionTickerInclude.php?anID"></script></em></span>
</div>
<?php
}
KennyP
11-06-2010, 10:54 PM
Thank you bluewalrus. I used Nile's code just as you recommended and it's working perfectly.
Thanks again BeverleyH, Nile and bluewalrus for all the replies.
Kenny
No problem! Sorry for not responding before. This happens me to where it tells you you need to log in, and when you go to log in your message is gone. When you get this error message it's better to copy everything in the text box, and then log in again, and then paste.
KennyP
11-07-2010, 08:41 AM
Hello again guys:
Thanks again for your help. Your script of course works perfectly. However, I had to re-open the topic as I hit the following snag, and I just can't solve it. Therefore I beg your help again.
The index page on the site I'm working on checks for a cookie. If not found, it redirects to a welcome page, and in turn, the welcome page redirects back to the index page.
By now, the cookie for the two divs has already been set, counting as the first visit. Therefore, the first time div never gets displayed.
As I see it, in order for this to work, the redirect to welcome page cookie code would need to precede the two divs cookie code, as follows:
<script type="text/javascript" src="scripts/cookie.js"></script>
<?
//test for existence of cookies... if the cookie from welcome page has not been set...
if(!$_COOKIE[userlogin]){
?>
<script type="text/javascript">
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'none', '', '/', '', '' );
// if Get_Cookie succeeds, cookies are enabled, since the cookie was successfully created.
if ( Get_Cookie( 'test' ) )
{
cookie_set = true;
// name, path, domain
// make sure you use the same parameters in Set and Delete Cookie.
Delete_Cookie('test', '/', '');
window.location = 'welcome-index.php';
}
// if the Get_Cookie test fails, cookies are not enabled for this session.
else
cookie_set = false;
</script>
<?
}
?>
<?php
$revisit = false;
if(isset($_COOKIE['div'])){
$revisit = true;
} else {
setcookie('div', true, 5184000 + time()); //hold fo 2 months
$revisit = false;
}
?>
<!-- END cookies -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
...however, in the above order the index page generates... "Warning: Cannot modify header information...(output started...index.php) in .....index.php on line..."
Is there a way to write the two codes so that they can be left in the above order?
Alternatively, if the order of the two codes is reversed, can the divs cookie code be altered to display the first time div on the second visit, and then to display the second time div as of the third visit onward?
If it helps, this is the site I'm currently working on...
billyjoeconor.com (http://www.billyjoeconor.com)
Thanks,
Kenny
KennyP
11-07-2010, 10:58 AM
Hey guys,
I actually got it to work. I used a PHP script for the re-direct to the welcome page. When I entered them as two separate scripts (one following the other) it did not work. But when I removed the closing tag of the first script, and the opening tag of the second script (leaving them as one script as you see below), it works. I'll leave it as it is unless you see something wrong with it.
Thanks again guys. I couldn't have done it without your help...but I guess you already knew that! :)
On the home page:
<?php
if(!isset($_COOKIE['no_splash'])) {
// If no cookie, take them to the splash page.
header ('Location: welcome.php');
exit;
} else
{
}
// Determine which div should display.
$revisit = false;
if(isset($_COOKIE['div'])){
$revisit = true;
} else {
setcookie('div', true, 7200 + time()); //hold for 2 hrs
$revisit = false;
}
?>
On the splash page:
<?
setcookie('no_splash', 'anonymous', time()+7200);
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.