Click here to Skip to main content
15,891,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I need to secure a profile page [Here is the link] Well anyone can access it and I don't want that to happen I want that page to stay closed/or locked until someone has logged on. My website has memberships and I want to protect the memberships from people logging in to it or they will be able to use the membership without paying.
Here is my code for profile.php:

PHP
<?php
//You check if the user is logged in or not
if($user_logged_in == true)
{
    require 'profile_loggedin.php';
}
else
{
    require 'profile_notloggedin.php';
}
?>


Here is the code for user_notloggedin.php

PHP
<?php
include('cn.php');
?>

HTML
<!DOCTYPE html PUBLICK "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
</head>
<body>
<form action="profile.php" method="post">
	<p>Username:</p>
	<input type="text" name="userUsername" col="50" />
	<p>Password:</p>
	<input type="password" name="userPassword" col="50" />
	<br />
	<input type="submit" value="login" />
	<br /><br />
	<a href="register.php">Register</a>
</form>
<a href="http://lulzhost.net76.net/advlogin/contact/index.html">Or go here to get a account that works</a>
<a href="http://lulzhost.net76.net/mm/"><h3>Don't want an account go here to watch some of the vids we have or post a vid</h3></a>
</body>
</html>
Posted
Updated 21-Oct-13 6:53am
v3
Comments
Captain Price 21-Oct-13 13:05pm    
Where is the code to determine the value of $user_logged_in in profile.php ?

1 solution

It's very simple. You just have to check weather the user is logged in or not. If logged in you render the page, if not you do not render the page.

XML
<?php
//profile.php

//You check if the user is logged in or not
if($user_logged_in == true)
{
?>
    <!--User is logged in. Render your secure page!-->
    <p>Welcome, You're logged in to a secure page</p>
<?php
}
else
{
?>
    <!--User is not logged in. Print an error message!-->
    <p>Please log in to continue</p>
<?php
}
?>


A Technique: A better way is you can create 3 files.
-- profile.php
-- profile_loggedin.php
-- profile_not_loggedin.php

Then,

C#
<?php
//You check if the user is logged in or not
if($user_logged_in == true)
{
    require 'profile_loggedin.php';
}
else
{
    require 'profile_notloggedin.php';
}
?>


In profile_loggedin.php you can write code for the logged in mode of the profile page and in profile_not_loggedin.php the not logged in mode of the profile page.
 
Share this answer
 
v2
Comments
Braydon 21-Oct-13 12:23pm    
DO I ADD this code the profiles page?
Captain Price 21-Oct-13 12:26pm    
Yes. You should include all your code in your profile inside this If-Else.
Braydon 21-Oct-13 12:27pm    
OK I did but it still shows the content but it was correct when I was logged out.
Braydon 21-Oct-13 12:29pm    
And It wouldn't show when I was logged in.
Captain Price 21-Oct-13 12:35pm    
See my edit (The technique). It should work (100% sure). If not there must be some error with your code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900