Jump to content

Password Protect page


kipper1960

Recommended Posts

Hoping someone can shed some light as I am new to php

 

I have set up a login for members on my site and all this works well, once logged in it sends me to a members page where I have used the following code to protect the page but for the life of me I cannot get it to work,  could some one help me out

 

<?

session_start();
if(!isset($_SESSION['username']) || $_SESSION['username']=="")
{
echo "Please login to see this page.....";
}
else
{
content();
}
 
function content()
{
   My members page information I thought goes in here
 
}

?>

Link to comment
https://forums.phpfreaks.com/topic/280108-password-protect-page/
Share on other sites

What exactly doesn't work?  The first thing to note is that the function you declare needs to go before the call of the function in your else{}.  Remember that php works top-to-bottom, so if you try to use a var or function before it's actually declared it won't work.  Also you would have know this if you have error_reporting(E_ALL) turned on.

This is the correct way to use the code you have.

session_start();
// Notice that the function declaration is now above where you call the function.
function content()
{
  echo 'My members page information I thought goes in here';
}

if(!isset($_SESSION['username']) || $_SESSION['username']=="")
{
	echo "Please login to see this page.....";
}
else
{
        // This is considered a function call below.
	content();
}

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.