?
Solved

PHP Page Counter Then Redirect User to Different URL

Posted on 2011-04-25
9
Medium Priority
?
442 Views
Last Modified: 2012-08-13
Hi All,

I need to run some tests to make sure a banner ad is tracking properly. I'd like to set up a page that, if visited, writes to a file and then redirects the user to a new URL (without the user noticing anything). I have this php file:

counter.php

<?
header( "HTTP/1.1 301 Moved Permanently" );
header( "Status: 301 Moved Permanently" );
header( "Location: http://www.domain.com/" );
exit(0); // This is Optional but suggested, to avoid any accidental output
?>
<?php
$filename='count.txt';

/*script for reading the text file*/

$line=file($filename);

/*script for adding 1 to the file*/

$fp=fopen($filename,'w+');
fwrite($fp,($line[0]+1));
fclose($fp);
?>

------------------------------

And a file called "count.txt" to write to in the same directory as the count.php file (with permissions set properly). When I type in the URL of count.php it redirects properly to domain.com, but the text file always shows "1". It doesn't seem to increase the count. This has to be something terribly stupid on my part.

Might anyone have an idea on how I can remedy this and get it to work right?  I just need to see a simple page view count in the text file for comparison purposes.

Many thanks!!
0
Comment
Question by:azlumiere
  • 5
  • 4
9 Comments
 
LVL 8

Expert Comment

by:PranjalShah
ID: 35463507
Do the redirection after you add the counter to the txt file.
0
 

Author Comment

by:azlumiere
ID: 35463513
Hi PranjalShah,

I thought of that too, and tried moving the redirect down, but then I get an error on the page:  Warning: Cannot modify header information - headers already sent by

Is there another way to do a redirect that won't break the page? This is the only way I know to set it up.

Thank you for responding!!
0
 
LVL 8

Expert Comment

by:PranjalShah
ID: 35463524
Other way is you can do the counter on the http://www.domain.com page itself. You can check the referrer for that page. If the $_SERVER['HTTP_REFERER'] is your first page than do the counter.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:azlumiere
ID: 35463552
Well, unfortunately in my case domain.com is the advertiser's website, so I have no control over it. I have to try and get it to work before I get the user to the advertisers site.
0
 
LVL 8

Accepted Solution

by:
PranjalShah earned 600 total points
ID: 35463598
0
 
LVL 8

Expert Comment

by:PranjalShah
ID: 35463608
Here is one more example. You can do the counter and then place this code in your script.

http://www.web-source.net/javascript_redirect.htm
0
 

Author Comment

by:azlumiere
ID: 35463667
Ah, very cool. I will take a look at this...
0
 

Author Comment

by:azlumiere
ID: 35463688
Brilliant! Worked like a charm!
0
 

Author Closing Comment

by:azlumiere
ID: 35463690
Thanks for your help!!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question