tags:

views:

57

answers:

4

How should I write the $location variable into the header line? Right now it finds nothing... Blank adress bar...

Here's what I have so far:

  $location='/ad?ad_id='.$id_nr;
  Header( "Location: $location" );

BTW, this is on my computer, virtual server, the adress is absolute!

It works without the ?ad_id=.$id_nr part!

A: 

The Location header must be a fully-formed absolute address, starting with http:// or some other protocol like https. You cannot specify a relative URL.

Try this, with mysite.com replaced with the root URL for your site:

<?php

$location='/ad?ad_id='.$id_nr;
Header( "Location: http://www.mysite.com/$location" );
// or, on your local machine
Header( "Location: http://localhost/$location" );

?>

You might consider eventually writing a more useful function to do the work for you:

<?php

function redirect_to($location, $halt = true) {
  $root = $_SERVER['HTTP_HOST'];
  header("Location: $root/$location");
  if ($halt)
    exit;
}

?>
meagar
Its on a virtual server on my computer... It works exactly like this but without the variable extension on another page... So it is absolute
Camran
no, relative paths can be specified too
Sarfraz
@Camran The virtual server on your machine still has an address, like localhost or 127.0.0.1. Try http: // localhost/ad?ad_id=...
meagar
No, relative paths cannot be specified (although most browsers will error correct (some showing an error report as they do so))
David Dorward
+3  A: 

Well, the header function is header not Header and the URI in location headers must be absolute, not relative.

David Dorward
A: 

In your location variable, i don't see a valid file extention eg html or php. Are you sure there is no extension of your files?

$location='/ad?ad_id='.$id_nr;
Header( "Location: $location" );
Sarfraz
A: 

Use $_SERVER['HTTP_HOST'] to get the full url.

Don't forget to put an exit after the header call. The script won't stop automatically, you have to tell it to stop.

AntonioCS