Jump to content

php login and send elsewhere help please


jokerfool

Recommended Posts

Some of my pages that are used for members only, the content can partially be seen by non members, exposing content to them, is there a command I can add to the top of the page that forces the user to login on the login.php page, so they don't see the part content? Is there some code I can use to validate the person is already a member and if they have the cookie log them in, otherwise redirect to login.php page?

 

Thank you.

I would use sessions for this.

 

I use this for a site on which only I have admin permissions:

 

<?php
// Starting the session
session_start('username');

$username = ($_SESSION['username']);

if ($username !== 'insert admin persons login here')


{ 

header("Location: index.php");

}

?>

 

It sends unauthorised users back to the homepage. So I'm sure you could use this based on a certain permissions level rather that a specific login.

Hmmmm, I have a question, with the above code am I too change anything? When I add the above code, the issue I am having is once I have logged in and visit a members page, the page reloads the index.php page and is displayed in the members section instead of the actual page. Did I miss something?

You need to start the session again in the header of every page with member content. Then use code such as this where appropriate to restrict who can see it:

 

<?php

if(isset($_SESSION['username']))
    {
        // Code for Logged members

        // Identifying the user
        $username = $_SESSION['username'];

	echo "Hello $username ";

	?>

	<a href="logout.php"> Log Out?</a>

	<?php 
       
        // Information for the user.
    }
else
    {
        echo "Hello Stranger.";
   
    }
?> 

Archived

This topic is now archived and is closed to further replies.

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