PHP w/ Redirect w/ Header Question

UberNewb

Newbie
Joined
May 16, 2011
Messages
24
Reaction score
1
I'm trying to make a dynamic PHP redirect script, i've found some code but not completely helpful it gives me a forbidden error.

<?php
$pid = (varchar)($_GET['pid']);
header('Location: $pid');
?>

is the code in my php file i am passing $pid in the link address.

I cannot use javascript as some peeps disable this. any ideas?

TY
 
<?php
$pid = (varchar)($_GET['pid']);
$url = "http://www.mysite.com?page=" . $pid;
header('Location: $url');
?>

Try the above assuming your page structure is: http://www.mysite.com?page=2
Change according to page structure...
 
Sorry just re-read your original post, need beer...!!!

If you are getting a variable with $pid then you need to append it to a domain/page so something like

<?php
$pid = (varchar)($_GET['pid']);
header("Location: http://www.newdomain.com?$pid");
?>

Or are you saying that $pid contains the url you wish to redirect to?
 
$pid contains the URL i would like to redirect to ;) ty all for quick responses, gonna try some of these and see what happens/make changes as appropriate
 
$pid contains the URL i would like to redirect to ;) ty all for quick responses, gonna try some of these and see what happens/make changes as appropriate

I would echo out what $pid contains first then you can work out what you need, as it maybe missing something?
 
actually here is another option, i have the URL already stored in a variable on the previous page..

$product_link , they are clicking on a picture that is a hyperlink, but i am trying to cloak it to some level, when they click the picture link, can it call a php script to forward them?

TY all
 
actually here is another option, i have the URL already stored in a variable on the previous page..

$product_link , they are clicking on a picture that is a hyperlink, but i am trying to cloak it to some level, when they click the picture link, can it call a php script to forward them?

TY all

yes I do something similar on one of my sites, I have a go.php file which the image is linked to when a user clicks the image it loads the file which then stores some info in a db then redirects the user somewhere else.
 
Ok, after a lil bit of research I have found that I am getting the error because the host i'm using has a setting in ModSecurity that needs to be changed. Once that is done if anyone is interested I'll post correctly functioning code.
 
i'll be intersted mate, always interested in seeing how others code what i'm doing too.
 
below works, problem was with modsecurity as stated above.
<?php
$link=$file;
header ("Location: $link");
exit();
?>
 
below works, problem was with modsecurity as stated above.
<?php
$link=$file;
header ("Location: $link");
exit();
?>

cool, i doubt you need the exit statement as you are redirecting on the line above but hey thats just being picky....!!!!! LOL
 
Yes, my thoughts as well, but every reference I can find recommends doing it anyway as a just 'in case' lol, the other thing I did find was that since these are affiliate links and contain gargantuan amounts of ampersands was they needed to be replaced with %26. I just did a replace all w/ excel and re-imported my table and from there it worked. will have to write a routine to fix this though as I can see it causing a lot of problems when i scale this to other sites. also any opinions on if this hides the link from google? hrm
 
Yes, my thoughts as well, but every reference I can find recommends doing it anyway as a just 'in case' lol, the other thing I did find was that since these are affiliate links and contain gargantuan amounts of ampersands was they needed to be replaced with %26. I just did a replace all w/ excel and re-imported my table and from there it worked. will have to write a routine to fix this though as I can see it causing a lot of problems when i scale this to other sites. also any opinions on if this hides the link from google? hrm
You only need to replace the ampersands? You can just loop through the table and use str_replace to replace the ampersands with %26.

PHP:
$getLinks = mysql_query("SELECT `id`,`link` FROM `links` WHERE `link` LIKE '%&%'");
while ($linksArr = mysql_fetch_array($getLinks)) {
   $linkId = $linksArr['id'];
   $newLink = str_replace("&", "%26", $linksArr['link']);
   mysql_query("UPDATE `links` SET `link`='$newLink' WHERE `id`='$linkId'");
}
mysql_close();

Something like that. You can also do that when retrieving links for forwarding or when inserting links from your control panel if you have one.
 
Yeah my final result was actually eliminating the whole URL and just passing the product ID, wallah!

Yes, I could have done strreplace but im a very big noob when it comes to php, mostly learning from what I can find from other websites. TY!
 
Back
Top