Hi,

I am trying to redirect links through a php redirect. I am just about there but am having problems with the coding to pass the variable through from my page to the php script.

For example link on my page is:
http://www.website.co.uk/redirect.php?m=196591.html

This should display the page for HotelID 196591, the url of which is stored in a database.

The code for the redirect.php script I'm using is:

<?php
include 'connect.php';
$m = $_GET['m'];
$result = mysql_query("SELECT HotelID, HotelUrl FROM databasename ");
while ($row = mysql_fetch_array($result)) {  
if ($m == ".$row{'HotelID'}.") {$link = ".$row{'HotelUrl'}.";}
}
header("Location: $link"); 
exit();
?>

It works if I hard code HotelID and HotelUrl into the 'if' statement, but am looking to have the HotelID variable carried through from the web page so I don't need to hard code thousands of pages into the script.

Hope somebody can help.

Thanks, Gordon

Unless you've simplified your code for posting it in this forum, and as such some steps are missing, you have gone about the process of redirecting to that URL in a very long winded way.

A more succinct method might be:

include 'connect.php';
$m = (int)$_GET['m'];  // Assumed integer data type
$result = mysql_query("SELECT `HotelID`, `HotelUrl` FROM `table` WHERE `HotelID` = $m LIMIT 1");

if(mysql_num_rows($result)) {
    $hotel = mysql_fetch_assoc($result);
    header("Location: {$hotel['HotelUrl']}");
    exit(0);
} else {
    die('Invalid hotel ID specified');
}
    
?>

The difference here, is we're only retrieving the hotel we're interested in, rather than all hotels, and then finding the one we want after the event.

I think your problem might have been the . characters you had before and after your array values. Also, when including arrays directly in double quotes, they need to be wrapped in curly braces if using string indexes.

Hope this helps.

R.

Hi,

Thanks for your reply. I have tried your code but doesn't work completely.

The variable is picked up correctly and if I change the coding to below to test, it prints the url correctly so everything appears fine.

<?php
include 'connect.php';
$m = (int)$_GET['m'];  
$result = mysql_query("SELECT `HotelID`, `HotelUrl` FROM `uk` WHERE (HotelID = $m) LIMIT 1");

if(mysql_num_rows($result)) {
    $hotel = mysql_fetch_assoc($result);
    echo ("".$hotel{'HotelUrl'}."");
    exit(0);
} else {
    die('Invalid hotel ID specified');
}
    
?>

However, when I introduce the header line for the redirect instead of printing, it doesn't work?

<?php
include 'connect.php';
$m = (int)$_GET['m'];  // Assumed integer data type
$result = mysql_query("SELECT `HotelID`, `HotelUrl` FROM `table` WHERE `HotelID` = $m LIMIT 1");

if(mysql_num_rows($result)) {
    $hotel = mysql_fetch_assoc($result);
    [B]header("Location: {$hotel['HotelUrl']}");[/B]
    exit(0);
} else {
    die('Invalid hotel ID specified');
}
    
?>

It doesn't seem to like the coding as that is the only change made.

Any ideas?

Thanks, Gordon

The response headers must be sent to the browser before any other content is outputted. A good place to start would be to check that you are doing this.

If you're unsure, try turning on error reporting, and you can also call and check the return value for the headers sent function.

error_reporting(E_ALL);
var_dump(headers_sent()); die;

R.

yes..@blocblue is right..the header must be sent to browser before any content is outputted....

What you want ?
Want to pass any variables to redirecting page?

Thanks all - now working

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.