Jump to content

Having issues with restricting pages if not logged in


Go to solution Solved by requinix,

Recommended Posts

I am working on a browser game and still learning some in's and out's of PHP. I have it working where the user enters their info in and submits the form, it then posts this info to the DB. 

Once the user submits the info, the site redirects to the main page where it would display everything if you were logged in. If you are not logged in however, it should only echo "You must be logged in to view this page" and nothing more. 

 

As of right now it does echo this, but still displays the rest of the page. 

 

Here is what I have for code:

<?php session_start();
if(!isset($_SESSION['uid'])){
    echo "You must be logged in to view this page!";
}else{
    include("safe.php");
}
?>
(HTML code goes in after this)

Am I not using the correct use of if(!isset ?

Which "the rest of the page"? That HTML stuff you have after the PHP code? Yeah, that will execute because you made a decision to show either the message or the safe.php - says nothing about the HTML below. If you want to hide that too either put it in safe.php or make sure it's included inside the else clause

}else{
include("safe.php");
?>
(HTML code goes in after this)
<?php }
Edited by requinix
  • Solution

You don't have to "echo" it for the same reason you don't have to do that now: you simply drop into and out of PHP mode just about whenever you want. Really, it's exactly how I posted.

Which "the rest of the page"? That HTML stuff you have after the PHP code? Yeah, that will execute because you made a decision to show either the message or the safe.php - says nothing about the HTML below. If you want to hide that too either put it in safe.php or make sure it's included inside the else clause

}else{
include("safe.php");
?>
(HTML code goes in after this)
<?php }

This works great thanks, now to get my login to work properly so I can actually view this page you must be logged in to view ;)

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.