Hi People, need some help

Actually I wanna block an IP address from a certain country... I don't want them to access my home page which is index.php.... so I've included a file

"country-redirect.php" this file is placed at beginning of my index.php....

index.php code is like

<?php
require('country-redirect.php'); 
?>

<html>
....
....
<h1>ALLOWED</h1>

</html>

now the code in the file "country-redirect.php" will check for the IP address......... code is like ......

/* Some long code here.... and then */

if($country == 'PK')
{
header('Location: http://www.mysite.in/block.php');
}
else
{
header('Location: http://www.mysite.in/index.php');
}

Now the problem is PK people are getting blocked, that is fine......... but people from other countries say Japan... are directed to my home page which is index.php but index.php is again calling country-redirect.php because its initial code is

<?php
require('country-redirect.php'); 
?>

so its like going in a loop... redirect loop error like....

I mean chrome is giving me this error...

This webpage has a redirect loop
The webpage at http://www.mysite.in/a/index.php has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

header('Location: http://www.mysite.in/index.php');

I don't think you need the second redirection (to the index.php) at all. If the visitor is from the banned country (such as 'PK') they will be redirected by first redirection. other visitors will just stay at the index.php.

Thanx for the reply, but its showing a blank page....

It shouldn't according to what you posted. Can you post the whole index.php.

In simplified form your index.php should look like:

<?php
    if($country == 'PK')
    {
        header('Location: http://www.mysite.in/block.php');
    }
?>

<html>
....
....
<h1>ALLOWED</h1>

</html>

and visitors from countries other than PK should se the ALLOWED title

Member Avatar for diafol

I can weigh in with agrreing with broj1 and mwansari, that you do NOT need the second case (else). If you are including (requiring) the country-redirect.php file, the only action should be to bounce Pakistani users to the block page. Otherwise do nothing. If you get a blank screen, you've done something weird - have a look at echoing out variables as a form of debugging. The echoes will stop the header redirect, but you want to see the values held by the variables so that you can track down the error.

Thanx people, u guys are so helpful..... Yeah its working....

And broj1 you are a jewel......... Thanx Man....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.