2

I am using header('Location:') to redirect the user to another webpage. But, instead of overwriting the previous address on the address bar, the new one gets appended to the one already present. For example:

The address bar currently holds:

localhost/v2/admin

and there is header('Location:'.DIRADMIN.'login.php'); where DIRADMIN is a constant defined as

define(DIRADMIN,'localhost/v2/admin/');

Then, after the redirect, the new address bar would be

localhost/v2/admin/localhost/v2/admin/login.php

I thought it was because of the server, I am using. I was using Uniform Server Zero. But then I installed WAMP and the problem continues.

I am still a noob, I have no idea what is causing this and if the problem is because of mod_rewrite, then on both servers the module was active. I already checked some of the problems like redirect-PHP header(Location:..)

among others. I even did a google search for this but to no end. How can I solve this problem?

header_remove();

will not work as the first address was typed in manually and not set by header();

Community
  • 1
  • 1
rathorsunpreet
  • 101
  • 1
  • 5

2 Answers2

7

Without the http prefix, browser is trying to find the path relative to your current one

Add "http://" as so

header('Location: '.DIRADMIN.'login.php');

If you want URL to be relative to your domain root path, you can just add '/'

header('Location: /'.DIRADMIN.'login.php');

You should always try to use a relative path - That way, if you change your domain, your code still works.

Skarlinski
  • 2,419
  • 11
  • 28
0

Define

define('DIRADMIN','localhost/v2/admin/);

As

define('DIRADMIN','http://localhost/v2/admin/');
PHP Worm...
  • 4,109
  • 1
  • 25
  • 48
  • 1
    Firstly, it should be `define("DIRADMIN"...` not `define(DIRADMIN...`. The latter method of using assumed strings is not recommended and PHP will issue notices about it (depending on level of error reporting selected). – Eborbob Jun 26 '15 at 09:48
  • 1
    Secondly, this is hard wiring your code to the `http` protocol. If you change to `https` at some point in the future you'll need to update all these instances. Either get the current protocol, or use protocol-relative URLS, e.g. `define("DIRADMIN", '//localhost/v2/admin/');` – Eborbob Jun 26 '15 at 09:52