Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts

    BASIC URL Redirect page

    I couldn't find a tutorial for what I was looking for but I'm sure this is extremely basic and someone can show me quickly.

    What I want to do is have a "links" page that just grabs a parameter from the URL and redirects the user to that page.

    So if I made a link "http://www.mysite.com/links.php?=http://www.google.com"

    Then my links page would grab the "http://www.google.com" and store it as a variable so I could print out... "You are leaving our site and being redirected to "$url" in 5 seconds". This should be simple but I'm not quite sure how to grab that variable from the header.

  • #2
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Well you can get the $url using $_GET which obtains data from the URL, then you can use a header() to re-direct the user.

    e.g:

    with url mysite.com/links.php?url=http://www.google.com

    PHP Code:
    $url $_GET['url']  //store the url from the address bar in a variable $url

    echo "<h1>We are going to redirect you to ".$url." in 5 seconds.......See ya !</h1>";

    sleep(5); //Let's go to sleep for a bit and wait before we redirect

    header('Location: $url'); //now lets redirect the user 
    You might want to delay longer and sanitize the data from the url (incase someone tries inserting some bad code) first...

  • #3
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    PHP Code:
    header('Location: $url'); 
    This wont work, because single quotes do not parse variables. Either concatenate it or use double quotes

    PHP Code:
    header("Location: $url"); 
    or

    PHP Code:
    header('Location: ' $url); 
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks guys, that worked great.

    I did run into one weird issue though.

    When I get a URL like:
    https://www. newsite.com/folder/Calculator.ASP?TYPE=Menu&OLG=Hyperlink2&WCU

    It truncates the $url variable at the "&" symbol in the hyperlink... so when I print it out on the page it appears like this "click here to go to https://www.newsite.com/folder/Calculator.ASP?TYPE=Menu"

    I lose everything after the "&" sign.
    Last edited by a4udi; 09-08-2009 at 05:24 PM.

  • #5
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Does it work if you change the & to &amp ?

  • #6
    Regular Coder Zangeel's Avatar
    Join Date
    Oct 2007
    Location
    public_html/
    Posts
    638
    Thanks
    17
    Thanked 79 Times in 79 Posts
    PHP Code:
    urlencode($url); 
    That ought to work
    PHP Code:
    $aString is_string((string)array()) ? true false// true :D 
    [/CENTER]

  • #7
    Regular Coder
    Join Date
    Jan 2005
    Posts
    189
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Cool, I also tried %26 to escape it but I'm guessing Zangeel's way is better practice?
    I'll have to try in a bit, the link I was sending it to is now down haha.

  • #8
    Regular Coder
    Join Date
    Apr 2008
    Posts
    685
    Thanks
    15
    Thanked 105 Times in 104 Posts
    Quote Originally Posted by a4udi View Post
    Cool, I also tried %26 to escape it but I'm guessing Zangeel's way is better practice?
    I'll have to try in a bit, the link I was sending it to is now down haha.
    I just did some testing and URL Encode changes & to %26 anyway..

    BTW you need to use the urlencode on the URL so it appears in the addressbar encoded.

    Anyway I am getting an error stating "Warning: Cannot modify header information - headers already sent by " etc.. because obivously the header statement is coming after the echo.

    How did you get around this just out of curiousity.

    Here was my testing code:

    PHP Code:
    <?php
    if($_GET['url']){
        
    $url $_GET['url'];  //store the url from the address bar in a variable $url
        
    echo "<h1>We are going to redirect you to $url in 5 seconds.......See ya !</h1>";
        
    sleep(5); //Let's go to sleep for a bit and wait before we redirect
        
    header("Location: $url"); //now lets redirect the user  
    }
    else echo 
    "<h1>No URL was Sent :(";
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •