Forum Moderators: coopster

Message Too Old, No Replies

Phrase for every 10 visits

         

Dexie

9:20 am on Feb 2, 2005 (gmt 0)

10+ Year Member



Hi all,

Michal referred me here - thanks Michal.

I'm trying to present the visitor to the website with a simple 2 line phrase. The phrase would be shown to the person who visits the home page every 10th time, so *not* for the person's 10th visit, it's for every 10th visit generally. Can anyone please let me know how I can do this please?

My hosts server can take cgi.

Any help appreciated.

Sev.

coopster

1:05 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome Dexie.

You could set a cookie to track the number of visits to your site which would be an integer value, then use modulo to check and see if it was a "10th" visit or not...

if ($_COOKIE['visit_number'] % 10 == 0) { 
print 'Hey! It's visit #' . $_COOKIE['visit_number'] . 'to our site!'
}

Dexie

1:32 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Many thanks Coopster, and thanks for the welcome.

Really looking to go the non-cookie route really, so that even the ones with cookies disabled can still see the phrase.

Any ideas?

Sev.

Sanenet

1:40 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either create a session variable, or write to a field in a database [best option], or write a flat file with the number of visits.

I'm sorry I can't write the code in PHP, but those are the three best options. Problems with the session variable is that if you restart the server it's lost, flat file can slow down the server if you're getting a lot of visitors.

The flat file is probably the best option - read it, get the number, if number = 10 then display message and reset to 1.

coopster

4:10 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Somehow you are going to have to remember who the person is that visited your site. If you're not using a cookie, you're going to have to ask them to sign-in and store the information on the server-side. Cookies just make it a ton easier.

Dexie

4:22 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Ok, looks like you're pretty persuasive ;-)

How do I get started? I've got about 1,000 phrases, how do I get every 10th visitor to see the phrases in turn?

Sev.

coopster

4:37 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe, I'm not trying to be persuasive, I just want you to understand how it all works together. The most important part is that you need to know who it is each time, right? And which visitation occurrence it is for that person. So, we need to know who we are working with on each visitation. How do we determine that? We can't look at the IP address because that can change. Somehow, the person must identify themselves. Login? Not if we don't have to for a simple application such as this. Then a cookie lends itself nicely to the situation. Make sense?

Getting started? Well, are you going to store the phrases in a database or a text file? I would begin by making that decision and getting that setup first. Then we can move on to setting a cookie, checking it's value, making decisions and updating it.

Dexie

4:39 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



Looking at that prev post, maybe I should clarify:

It would be for every 10th visit to the home page, whether it's the 10th visit by the same person, or 1 visit each from 10 people, it would be the same - have I explained that ok? ;-)

Sev.

coopster

4:41 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ah. Yes, that does indeed change things. We won't need to use cookies then.

OK, how are you going to store the phrases?

Sanenet

4:42 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but Coopster, it's for every 10th visitor, not the tenth visit of each person.

In which case, Dexie just needs a general stat counter which shows a phrase every ten clicks, not a user tracker.

Dexie

6:16 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



But how do I get the phrase from my pc to be viewed by the visitor on the 10th visit?

Sev.

coopster

6:44 pm on Feb 2, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That depends on where the phrases are stored ... you haven't answered the question yet ;)

kilonox

6:49 pm on Feb 2, 2005 (gmt 0)

10+ Year Member



I would just flat file stuff like this. Life is to short to use sql for this stuff.

Increment a single textfile with your total hits based on whatever you want to call a hit. Insert all your quotes into another textfile, one per line, two lines per quote, or two lines per quote and new line between the next two line quote- it doesnt really matter.

On every page request check the hits file number and see if its divisible by 10. From there either use rand() to grab one randomly, or increment another textfile with a incrementing integer and use that to return your phrases one at a time in order.

hth.

Dexie

8:16 am on Feb 3, 2005 (gmt 0)

10+ Year Member



Many thanks Coopster and Kilonox, lightbulbs getting brighter ;-)

Are we agreed that I'll put the phrases in a text file - (is notepad ok?) and I make sure that there's one line of empty space between each phrase - ok so far? What do I do then?

Sev.

coopster

3:41 pm on Feb 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So, there is basically two newlines between your phrases? You really only need one.
This is phrase one 
This is phrase two
This is phrase three
...but if you want two, that's fine...

This is phrase one 

This is phrase two

This is phrase three

Next step is to create an additional text file to store the hits. Do you want this to be a continuous counter so you can view the totals once in awhile, or just a 10-count rotation that will be reset to 0 after every tenth visit?

Dexie

3:55 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



Hi Coopster,

Not really fussed, either way would be fine please.

Sev.

coopster

5:15 pm on Feb 3, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



OK, let's write things out in plain language first. Then we'll code to the plan. A person visits your site and hits the page which is going to invoke the PHP script.
  1. Read the counter file
  2. Add 1 to the counter file value and update the counter file with the new value
  3. Check to see if it is a multiple of 10
  4. If so, open and read the phrases file
  5. Get the phrase out that you want to display
  6. Display the phrase to the end user.

We'll take a simple route here, no file locking; if you want file locking, use fopen() [php.net], flock() [php.net], fread() [php.net], fwrite() [php.net] and fclose() [php.net]. OK, here we go ...


<?php
$counterfile = 'counter.txt';
$phrasesfile = 'phrases.txt';
$multiple = 10;
// Read in the value, trim off any newlines, and
// convert it to an integer, just in case:
$text = intval [php.net](trim [php.net](@file_get_contents [php.net]($counterfile)));
// Update the file with the old value plus this hit:
@ [php.net]file_put_contents [php.net]($counterfile, $text += [php.net] 1);
// Is the value a mutliple of 10?
if ($text && [php.net] $text % [php.net] $multiple == 0) {
// Read the phrases file into an array:
$file = @file [php.net]($phrasesfile);
print 'Hey! It\'s visit #' . $text . '!<br />';
// Grab a random phrase from the file read in as an array:
print $file[rand [php.net](0, count [php.net]($file) - 1)] . '<br />';
}
?>

If somebody else wants to throw down the file locking version, feel free ;)

[edited by: coopster at 8:01 pm (utc) on Feb. 5, 2005]

Dexie

6:21 pm on Feb 3, 2005 (gmt 0)

10+ Year Member



Many thanks Coopster. Not sure if I've changed the correct bits, but in the main page, where the visitor would see the phrase, I have this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Phrases</title>

<?php $counterfile = 'counter.txt'; $phrases.txt = 'phrases.txt'; $multiple = 10; //
Read in the value, trim off any newlines, and // convert it to an integer,
just in case: $text = intval(trim(@phrases.txt($phrases.txt))); //
Update the file with the old value plus this hit: @phrases.txt($counterfile,
$text += 1); // Is the value a mutliple of 10? if ($text && $text % $multiple == 0) { //
Read the phrases file into an array: $file = @phrases.txt($phrasesfile);
print 'Hey! It\'s visit #' . $text . '!<br />';
// Grab a random phrase from the file read in as an array:
print $file[rand(0, count($file) - 1)] . '<br />'; }

</HEAD>

<body text="#000080" bgcolor="#FFFFFF">

Blurb
</body>

</html>

and then another file in the website a file is called phrases.txt that holds the phrases, but when I go to the page where the visitor is supposed to see the phrase, called, would you guess, phrases - there's an error message on the page:

Parse error: parse error, unexpected '=' in /usr/local/psa/home/vhosts/example.com/httpdocs/phrases/index.html on line 7

Any ideas what I'm doing wromg anywhere please?

Sev.

[edited by: coopster at 12:14 pm (utc) on July 14, 2005]
[edit reason] generalized url by request [/edit]

coopster

1:29 pm on Feb 4, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The script snippet offered was meant to be run on the page that would display the phrase. If you want to separate it into two pages, you would put the logic to determine whether or not we should be viewing a phrase in one page, and the actual retrieval and display in another page. One way to to do this would be to use the php header() [php.net] function to redirect the user to that page...

Landing Page

<?php 
$counterfile = 'counter.txt';
$multiple = 10;
// Read in the value, trim off any newlines, and
// convert it to an integer, just in case:
$text = intval [php.net](trim [php.net](@file_get_contents [php.net]($counterfile)));
// Update the file with the old value plus this hit:
@ [php.net]file_put_contents [php.net]($counterfile, $text += [php.net] 1);
// Is the value a mutliple of 10?
if ($text && [php.net] $text % [php.net] $multiple == 0) {
// Redirect the user to the Display Phrase page:
header [php.net]('Location: http://example.com/displayphrase.php');
exit [php.net];
}

Display Phrase Page

<?php 
$phrasesfile = 'phrases.txt';
// Read the phrases file into an array:
$file = @file [php.net]($phrasesfile);
print 'Hey! It\'s visit #' . $text . '!<br />';
// Grab a random phrase from the file read in as an array:
print $file[rand [php.net](0, count [php.net]($file) - 1)] . '<br />';
?>

Dexie

4:03 pm on Feb 4, 2005 (gmt 0)

10+ Year Member



Hi, many thanks for the efforts here. I was hoping for the phrase to preferably popup, when the 10th visitor visited the home page.

Sev.

coopster

6:51 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



That's what the syntax in message #17 offers.

You have some errors in the translated code you used. You are using filenames where functions are supposed to be for one.

The board broke my formatting tags in message #17, I'll see if I can get the formatting fixed. Follow the links used throughout the snippet to see how each function works. It's a nice little snippet to analyze and learn from.

Dexie

7:59 pm on Feb 5, 2005 (gmt 0)

10+ Year Member



Many thanks for your help, I must admit, I am floundering a little - if you do have that code, that would be brill.

Sev.

coopster

8:05 pm on Feb 5, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



It's already there, in message #17 of this thread. I'll add it to your HTML ...
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Phrases</title>
</head>
<body text="#000080" bgcolor="#FFFFFF">
<?php
$counterfile = 'counter.txt';
$phrasesfile = 'phrases.txt';
$multiple = 10;
// Read in the value, trim off any newlines, and
// convert it to an integer, just in case:
$text = intval(trim(@file_get_contents($counterfile)));
// Update the file with the old value plus this hit:
@file_put_contents($counterfile, $text += 1);
// Is the value a mutliple of 10?
if ($text && $text % $multiple == 0) {
// Read the phrases file into an array:
$file = @file($phrasesfile);
print 'Hey! It\'s visit #' . $text . '!<br />';
// Grab a random phrase from the file read in as an array:
print $file[rand(0, count($file) - 1)] . '<br />';
}
?>
Blurb
</body>
</html>
Save this page and hit refresh 10 times until you get your message. Should work fine, as long as you have your "phrases.txt" and "counter.txt" files in the same directory as this php script.

Dexie

10:04 am on Feb 6, 2005 (gmt 0)

10+ Year Member



Many thanks Coopster, I've done exactly as you suggested in your last post, but the phrase isn't coming up ;-( But I think it's something to do with the counter.txt file - could you please let me know what I need to put into that file please?

Your help is much appreciated.

Sev.

coopster

12:58 pm on Feb 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I started the "counter.txt" file out with a zero, but tested it with anything, including text. The intval() function is going to convert it to a number anyway.

Make sure the "counter.txt" file and "phrases.txt" file are in the same directory as this script! Also, I've coded the script to assume the "phrases.txt" file has only one newline per phrase entry:

This is phrase one 
This is phrase two
This is phrase three

Dexie

10:11 am on Feb 7, 2005 (gmt 0)

10+ Year Member




Thanks again Coopster. Just so that we're running along the same track, am I right in saying that there's 3 files needed:
1. The home page, (which contains the script), where the phrase pops up on.
2. The counter.txt file which must be in the same directory as 1 above.
3. The phrases.txt file which must be in the same directory as 1 above.

Is that about right, or have I left anything out please?

Also, I'll make sure that each phrase is on a separate line, but what do I actually put into the counter.txt file?

Sev.

coopster

12:18 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, you are correct on all three counts and the structure of the phrases.txt file. As stated, I simply started the counter.txt file off with a single 0 (zero).

Dexie

12:47 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



Hi Coopster, Thanks for taking the time to help me on this, it is appreciated.

I'm a little unclear on this bit:

"I simply started the counter.txt file off with a single 0 (zero)."

Do I put a zero somewhere?

Sev.

[edited by: coopster at 12:51 pm (utc) on Feb. 7, 2005]
[edit reason] removed url per TOS [webmasterworld.com] [/edit]

coopster

12:54 pm on Feb 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yep, you just create the text file, open it and key a 0 in there. Exit and save the file.

View Source after browsing to your page. Do you see the PHP code in there? That means that you aren't parsing PHP on your server. Are you sure you have PHP installed and running on your server?

Dexie

2:31 pm on Feb 7, 2005 (gmt 0)

10+ Year Member



I had to change the extn to .php to make sure it parsed.

But now I get this error message:

Parse error: parse error, unexpected T_STRING in /home/gh/public_html/phrases/index.php on line 5

This is the code I have in the landing page:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Phrases</title>
</head>
<body text="#000080" bgcolor="#FFFFFF">
<?php $counterfile = 'counter.txt'; $phrasesfile = 'phrases.txt'; $multiple = 10; //
Read in the value, trim off any newlines, and // convert it to an integer,
just in case: $text = intval(trim(@file_get_contents($counterfile))); //
Update the file with the old value plus this hit: @file_put_contents($counterfile, $text += 1); //
Is the value a multiple of 10? if ($text && $text % $multiple == 0) { // Read the phrases file
into an array: $file = @file($phrasesfile); print 'Hey! It\'s visit #' . $text . '!
<br />'; // Grab a random phrase from the file read in as an
array: print $file[rand(0, count($file) - 1)] . '<br />'; }?> Blurb
</body>
</html>

Any help to get this solved, much appreciated ;-)

Sev.

This 33 message thread spans 2 pages: 33