Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP IP Redirection Anyone!

  • 06-05-2008 4:06pm
    #1
    Closed Accounts Posts: 511 ✭✭✭


    Before I pull my hair out!

    Guys for the last three days i've been trying to write a simple PHP page to direct visitors base on their IP.

    All I need is:

    87.232.47.1 to be directed to www.google.ie
    and everyone else to be directed www.yahoo.com

    Anyone PLEASE!! At this stage i'll pay lol! :D


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    [php]<?php
    if(isset($_SERVER) && $_SERVER == "87.232.47.1") {
    header("Location: http://www.google.ie");
    } else {
    header("Location: http://www.yahoo.com");
    }
    exit();
    ?>[/php]


  • Closed Accounts Posts: 511 ✭✭✭Undercoverguy


    Where abouts in the PHP page should i be pasting that?


    I keep getting this following message when I try visit the page...

    PHP Warning: Cannot modify header information - headers already sent by (output started at D:\hshome\lint\mysite.ie\3.php:4) in D:\hshome\lint\mysite.ie\3.php on line 8


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    Should be the very first thing in the file, or at least before any html AFAICR.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    That code will need to go at the absolute top of your script, or in more accurate terms, it must be read by the browser before any html is output by the web page. This means before your <html> tag.


  • Closed Accounts Posts: 511 ✭✭✭Undercoverguy


    Thanks for the code :D

    I have it in place on a PHP page...
    but for some reason its not working.

    It just goes straight to the 'else' page.
    Its almost as if it cant pick up the IP of the visitor.

    I've tried this on three different lines here in the office with no joy.
    (Three different IP's)

    Any ideas? :confused:


  • Advertisement
  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Try simply:

    [php]print ('Your IP is: '.$_SERVER.'.');[/php]

    to ensure your are retrieving the IP.

    And if you're testing this on a local environment, your IP address won't be that which it would be while browsing the internet!


  • Closed Accounts Posts: 511 ✭✭✭Undercoverguy


    Ah, Right.

    Looks like our ISP is routing us through a proxy.

    Any way to get around that?


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    Where abouts in the PHP page should i be pasting that?


    I keep getting this following message when I try visit the page...

    PHP Warning: Cannot modify header information - headers already sent by (output started at D:\hshome\lint\mysite.ie\3.php:4) in D:\hshome\lint\mysite.ie\3.php on line 8

    As Mirror already noted,

    You cannot echo anything prior to calling a PHP re-direct via header modification.

    You have to re-direct the user before echoing to the web page.

    HTTP protocol only responds once to a request. Once the headers are sent, they cannot be resent by the server unless a new request is made.

    There are two ways around this:
    i) use javascript to re-direct
    ii) put <?php ob_end_flush(); ?> and <?php ob_end_flush(); ?> at the very beginning and very end of your .php/.html file. Doing this buffers the whole page and sends the HTTP request all at once (re-direct headers included).


  • Closed Accounts Posts: 511 ✭✭✭Undercoverguy


    Well I seem to have the page working okay as I've it written in PHP format, the Problem is the ISP is routing us through a proxy. So is there a way of pulling the true ip using PHP?


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    No, not really.

    Best you can do is get the IP(s) of your ISP's proxy(ies) and check for all of them.


  • Advertisement
  • Closed Accounts Posts: 511 ✭✭✭Undercoverguy


    Ah I figured it out and it seems to work.
    Just replace

    [PHP]if(isset($_SERVER) && $_SERVER == "87.232.47.1") {[/PHP]

    With this:
    [PHP]if(isset($_SERVER) && $_SERVER == "87.232.47.4") {[/PHP]

    It seems to work anyway :D

    Cheers Guys for all your help! Really appricate it! :pac:


  • Subscribers Posts: 9,716 ✭✭✭CuLT


    Imagine seem to largely use 87.232.1.48 as their proxy server. That will probably cover every imagine customer who visits your page. If that's not a problem, work away.

    Edit: never mind :)


  • Registered Users Posts: 345 ✭✭justindublin


    Hey all,

    Trying to edit this code so to work on a local IP.

    for example when the script runs everyone one

    192.168.50.1 goes to pageA
    everyone else goes to pageB

    Any ideas?


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Hey all,

    Trying to edit this code so to work on a local IP.

    for example when the script runs everyone one

    192.168.50.1 goes to pageA
    everyone else goes to pageB

    Any ideas?
    surely just changing the IP check to the relevant IP address, no?


  • Registered Users Posts: 345 ✭✭justindublin


    Dont think so, because the code checks for the line/dsl IP and not the local IP.
    So if I do that everyone will go to pageB as no visitor IP's will match 192.168.50.1


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Dont think so, because the code checks for the line/dsl IP and not the local IP.
    So if I do that everyone will go to pageB as no visitor IP's will match 192.168.50.1
    well that depends on how they are accessing it. if via the www then you're correct, if via a local intranet then no.

    i don't know of a way to get an internal IP of someone accessing your site on the internet, i would imagine it would be quite the safety flaw...


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Mirror wrote: »
    i don't know of a way to get an internal IP of someone accessing your site on the internet, i would imagine it would be quite the safety flaw...
    Some proxies pass this information forward. I don't think it's much of a secutiry problem. A worst it saves a potential attacker a few seconds.

    Most home DSL/Cable routers don't pass the info forward, so there's no way of telling that it's an internal IP if it's an external web server.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    seamus wrote: »
    Some proxies pass this information forward. I don't think it's much of a secutiry problem. A worst it saves a potential attacker a few seconds.

    Most home DSL/Cable routers don't pass the info forward, so there's no way of telling that it's an internal IP if it's an external web server.
    yes, a bit of googling there revealed as much.

    OP, are you sure you can't use another method?


  • Registered Users Posts: 345 ✭✭justindublin


    What other methoads are available?

    If i was to host the redirect/filder page locally what needs to be changed?
    or would the existing code work?


  • Registered Users Posts: 345 ✭✭justindublin


    Anyone know know to add multipal IP's to this argument?


  • Advertisement
  • Registered Users Posts: 9,579 ✭✭✭Webmonkey


    Anyone know know to add multipal IP's to this argument?
    Just add them to an array and do a for loop checking it against the remote host address. If you get a match then, do the neccessary action.


Advertisement