Jump to content

issue with checking login sessions..


decon

Recommended Posts

So, I'm kinda new to PHP I started learning it a couple months ago and I'm currently trying to make a login/register system with a user control panel, admin backend etc... I've got the registration part down I can insert users into the database, etc but I'm still sorta a noob to this.

 

So basically I'm trying to check if the user is logged in using $_SESSION like everyone does, well mostly I think.

 

Anyway here is the code I'm using for my file login_check.php:

<?php
session_start();
if ( isset( $_SESSION[ 'logged_in' ] &&  $_SESSION[ 'logged_in' ] == true ) ){
  echo "You are logged in<br/>";
}else {
  echo "You are not logged in<br/>";
}
?>

once I go to login_check.php on my website the page is completely blank, and as far as I'm aware a blank page in PHP means there is a error in the code. but I've tried to fix the errors, I've narrowed it down to being the 3rd line. Something is wrong with it, but I'm not sure what. 

 

Please help. Thanks guys.

Link to comment
Share on other sites

Turn on error reporting at the top of your script:

ini_set('display_errors',true);
error_reporting(-1);

These lines instruct php to print all errors and warnings to the screen, so you'll them instead just a blank white screen. Obviously you'll want to comment these lines out before you take your site live, but you should always have error reporting enabled while developing.

Link to comment
Share on other sites

your code has the closing ) for the isset(...) statement in the wrong place (at the end of the line), producing a php syntax error (Parse error: syntax error, unexpected '&&' (T_BOOLEAN_AND), expecting ',' or ')' in your_file on line 3). the closing ) for the isset() belongs after the $_SESSION[ 'logged_in' ] parameter - isset( $_SESSION[ 'logged_in' ] ) && ....

 

you need to have php's error_reporting set to E_ALL and display_errors set to ON in the php.ini on your development system to get php to help you find these kind of errors. putting these settings into your code won't help with syntax errors in your main file since your code never runs when there is a syntax error and lines trying to change the settings aren't executed.

 

@sford999, while your posted code is syntactically correct, by removing the isset() statement, it will throw php errors when the page is visited when not logged in. also, by just posting 'fixed' code, without any statement of what was wrong with the original code, the OP doesn't learn anything.

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.