0

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

4
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by gordo58
1

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.

Edited by blocblue: n/a

0

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

0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.