Jump to content

user is online?


wkdw1ll1ams

Recommended Posts

i got my user login and register working with my sql :) but now if a non logged in user tries to access the shoutbox i want it to redirect them to the register page.

 

<?PHP

session_start();

if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {
header ("Location: /login/main.php");
}

?>

 

 

 

im using that code but even if im logged in, it redirects me to the register page?  :shrug:

 

my main site is on the root of the site. the login page and the logged in page is in "/login/

Link to comment
Share on other sites

Your NOT operator, exclamation point, is on the wrong side of the parentheses.  Also there is no space between the session_start(); and the initial php tag, but I am guessing that's the way it was copied and pasted.

Also, you can eliminate one of the parentheses as there are not multiple logic scenarios in your if statement.

 

Before:

 

<?PHPsession_start();if (!(isset($_SESSION['login']) && $_SESSION['login'] != '')) {header ("Location: /login/main.php");}?>()

 

After

 

<?PHP session_start(); if (!isset($_SESSION['login']) && $_SESSION['login'] != '') {header ("Location: /login/main.php");}?>()

Link to comment
Share on other sites

Your NOT operator, exclamation point, is on the wrong side of the parentheses.  Also there is no space between the session_start(); and the initial php tag, but I am guessing that's the way it was copied and pasted.

Also, you can eliminate one of the parentheses as there are not multiple logic scenarios in your if statement.

 

No, his NOT operator is correct for what he has, but the whole construction of the condition is kinda backwards IMHO. Also, not sure what you are saying about no space between the opening PHP tag and the session_start() - they are on two separate lines from what I see.

 

Anyway, I agree with xyph do a print_r(0 to check the session variable. I would also change the logic to be easier to interpret.

<?php

session_start();

if (!isset($_SESSION['login']) || $_SESSION['login'] == '')
{
    //Debug line
    echo "Auth check failed. Session vars: " . print_r($_SESSION, true);
    //header ("Location: /login/main.php");
}

echo "Auth check passed";
?>

Link to comment
Share on other sites

ive attached the login files in a zip archive for you to understand more of how the code works.

 

Have you verified the content of $_SESSION like we suggested? What were the results. Don't just throw code at us expecting us to fix it. My goal on this forum is to help people and in that process to learn to solve problems on their own. A big part of that is learning to debug your code. When something does not work the way you expect then you need to verify the inputs and outputs. Since your if() statemetns seems to be not working you need to validate the values that are being compared in that if condition.

Link to comment
Share on other sites

im not firmiliar with $_session but i just get an error:

 

Notice: Undefined variable: _SESSION in C:\xampp\htdocs\tvshows.php on line 2

 

Are you trying to use $_SESSION before you start the session (i.e. did you put print_r($_SESSION); before session_start())?  Try the code I posted and state what the results are.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.