Forum Moderators: coopster
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.
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.
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.
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.
This is phrase one...but if you want two, that's fine...
This is phrase two
This is phrase three
This is phrase oneThis 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?
<?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]
<!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]
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 />';
?>
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.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"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.
"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>
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
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.
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]
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.