March 8, 2012

PHP Redirect Within website

Question by baburao113

I’ve been trying this code since 3 days in a row until a certain time (minutes or seconds) but unable to solve the problem.

My target is to redirect visitor to 10 random URLs which are being selected from a text file. The user will see a certain page for a certain time and then redirect to another page again, the number of pages he will be redirected to is complete RANDOM.


The problem is the visitor is not being redirected to any other page which is randomly selected from a text file, instead it is just refreshing the page… But I want to redirect him to other pages from the text file.. Hope you guys understood me by now.

EDIT: Found the problem. Actually the $rand_link is having NULL as it’s value.. { [0]=> NULL } Don’t know why…. ANy solution? Checked the ‘BBnormalLinks.txt’ file for it’s permissions and that file is having some links in it for sure because I just checked it..


Here is the CODE:

<?php // Generate Random Nubmers.. 2 ********
            $numbers2 = range(13,70);


            for ($j=0;$j<1;$j++)
                $seconds = numbers2[0];

            //////// For Random URL of Site
            $links = file('BBnormalLinks.txt');
        $rand_link = $links[ mt_rand(0, count($links) - 1) ];                   

                header("refresh:". $seconds .";url=". $rand_link); ?>

Answer by Starx

The syntax is correct but some pointers that can cause this are

  1. Some text have been outputted before the header is passed.
  2. The random page, is not being generated, thus ending up refreshing the same page again and again.

I have a very strong feeling, that your $rand_link is returning blank or null.


After a few discussion, the problem was the evil path again.

$links = file('patotofileBBnormalLinks.txt');

As baburao113, quoted

I had to move that file to wordpress theme folder lol! Problem resolved 🙂

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+


Please fill the form - I will response as fast as I can!