Jump to content

Redirect from header with conditional statement


estreetfan999

Recommended Posts

Hi,

 

Why doesn't this work? Driving me nuts! Thanks.

 

<?php

    if($_SERVER[php_SELF] == 'somepage.php')

{

    Header("Location: http://www.thissite.com");

    } else {

    Header("Location: http://www.anothersite.com");

    }

?>

 

The code NEVER redirects to the first URL (thissite.com). It ALWAYS redirects to  the second URL (anothersite.com).

 

What the heck am I doing wrong? 

 

First, you need to use quotes around strings. Since associative arrays use strings for keys, you need quotes around the key 'PHP_SELF'.

 

Second, have you echo'd $_SERVER['PHP_SELF'] to verify it contains what you expect?

 

It probably doesn't contain 'somepage.php', otherwise it would execute the first header() call.

Thanks.

 

I'm using an include file, which has this redirect code in it. 

 

<?php require_once('include-test.php'); ?>

 

Placed this include file at the very top of somepage.php

 

I added the quotes around 'PHP_SELF'

 

Still going to second link and ignoring the first link.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.