page redirect if not logged in

i have a session page that i call on all of my pages that the user must be logged in to view. i create the session with the user id then i tried to make a line of code to redirect is there was no value in the session this is the code that i have tried to use

<?php
if (($_SESSION['user_ref'])<"0"){
  ( pageaddress=="first.php" || pageaddress=="second.php" || pageaddress=="third.php"); {
  header ('index.php');
}
}
?>

i have only been using php a couple of weeks so this is all new to me and i greatly appreciate the help

What’s this line doing:

[php]( pageaddress==“first.php” || pageaddress==“second.php” || pageaddress==“third.php”); {[/php]

Besides that, your header() parameter is incorrect. Check out http://www.php.net/header (that should work).

that line is listing the pages that i want restricted unless the user is logged in

Are you sure about that? :wink:

Try using error_reporting(E_ALL); to verify your syntax.

I am wondering about this code, does it give any errors? This doesn’t look as though it could be correct syntax, but could be wrong.

[php]<?php
if (($_SESSION[‘user_ref’])<“0”){
( pageaddress==“first.php” || pageaddress==“second.php” || pageaddress==“third.php”); {
header (‘index.php’);
}
}
?> [/php]

They way I would write would be has such:

[php]<?php
if (($_SESSION[‘user_ref’])<“0”)
{
if( $pageaddress == “first.php” || $pageaddress == “second.php” || $pageaddress==“third.php”)
{
header (‘index.php’);
}
}
?> [/php]

I have tried using this format but i can still view the page i am somewhat stuck i appreciate your time

<?php 
if (($_SESSION['user_ref'])<"0") 
{ 
    if( $pageaddress == "first.php" || $pageaddress == "second.php" || pageaddress=="third.php") 
    { 
        header ('index.php'); 
    } 
} 
?> 

@Ragster:
If it is correct syntax, it would be like writing the following:

[php]<?php
if (($_SESSION[‘user_ref’])<“0”){
true; // OR FALSE, WHICHEVER
{ // NOT SURE IF PHP ACCEPTS CODE BLOCKS LIKE THIS
header (‘index.php’);
}
}
?>[/php]

So it wouldn’t do much anyway, if the code block between curly braces is allowed in PHP (I know they are in Java).

@Professor:
Your page isn’t redirecting because your header() parameter is still faulty. The link I gave you earlier gives a perfectly working example of how a header redirect should be implemented (and it works, because I’m using it myself).

@Zypppora
I guess I just don’t know how it could be correct syntax as

pageaddress wasn’t set as a variable and I thought that if you are going to validate a string against another string they should both be double quoted.
“pageaddress” == “page1.php” which to me really doesn’t make sense, becuase it would always validate false. So I figured it must be a variable which would need the dollar sign for php to know that.

I just don’t know how this script even got through without passing any errors. I would venture to guess that error reporting is off?? And in the least its not displaying warnings?

I am guessing in the beginnning had the code had some sort of debugging in place professor would have found that the script was never validating or getting to the header and found there was something wrong with the if statement condition.

@professor
I would use error_reporting() function to make sure all errors are being displayed, unless you know for sure they are.

I would double check the structure of the condition in the if statement, I noticed mine had still missed one $.

Then I would get the correct syntax for the header function as Zyppora stated.

Correct/Check all of this and it should work.

Actually, the pageaddress == “page1.php” would validate if and ONLY if pageaddress were a constant. Constants are generally written in ALLCAPS but that’s not a rule IIRC. But you’re right that PHP tries to pass it off as a string if it can’t find any definition.

once again thanks for all of your tips and help

Sponsor our Newsletter | Privacy Policy | Terms of Service