tags:

views:

56

answers:

2

Please look at the example code below, when I run it, the defined part is nottranslated in the header function call, is there a special way to do the syntax or is this exact method not possible?

<?PHP
session_start();
define('SITE_URL', 'http://testsddf.com');
$_SESSION['user_role'] = 0;


//if a user is not active, redirect to verification/suspended page
if($_SESSION['user_role'] == 0){
    header('Location: SITE_URL');
}

?>
+10  A: 

You cannot do something like this :

define('SITE_URL', 'http://testsddf.com');
header('Location: SITE_URL');

A constant is not interpolated, inside of a string : it's not a variable (and you are using single-quote string, so it wouldn't work with a variable either, here)


You have to use string concatenation, in this situation :

define('SITE_URL', 'http://testsddf.com');
header('Location: ' . SITE_URL);


And, just to put a link to the manual as a reference, you can take a look at Variable parsing : there is nothing there about constants -- even if that's unfortunate.

Pascal MARTIN
hello, I tried your method as well but it still will not work with the defined variable, assuming that this is the only method header('Location: ' . $SITE_URL); and not header('Location: ' . SITE_URL); and then define $SITE_URL instead of SITE_URL?
jasondavis
If you are using a define (and, this way, creating a constant), you should not use $ : the $ symbol is for variables only.
Pascal MARTIN
Yes I understand that part, it's just that header('Location: ' . SITE_URL); is not working even with define('SITE_URL', 'http://testsddf.com');
jasondavis
weird it just started working, must of been some sort of caching issue, thanks
jasondavis
Strange ^^ But great it's working, now.
Pascal MARTIN
A: 

You either need to do...

header('Location: ' . SITE_URL);

...or...

header("Location: SITE_URL");

...as single quotes don't expand variables. (See this page for more info.)

middaparka
I have tried this already but it does not seem to work with a defined variable for some reason
jasondavis
That's because it is not a "defined variable", it is a constant. When you are inside a string, anything without a variable reference identifier (`$`) is already a constant. You must break out of a string constant to access the contents of a defined constant.
Dereleased