...

View Full Version : BASIC URL Redirect page



a4udi
09-08-2009, 01:56 AM
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.

Scriptet
09-08-2009, 02:08 AM
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



$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...

Zangeel
09-08-2009, 02:56 AM
header('Location: $url');

This wont work, because single quotes do not parse variables. Either concatenate it or use double quotes


header("Location: $url");

or


header('Location: ' . $url);

:D

a4udi
09-08-2009, 04:22 PM
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.

Scriptet
09-08-2009, 04:57 PM
Does it work if you change the & to &amp ?

Zangeel
09-08-2009, 05:35 PM
urlencode($url);

That ought to work

a4udi
09-08-2009, 05:39 PM
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.

Scriptet
09-08-2009, 05:55 PM
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
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 :(";
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum