If statment to say if the user is logged in or not.

Post here is you are having problems with any of the tutorials.
Post Reply
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

If statment to say if the user is logged in or not.

Post by wignall6 »

Hello,

On the user login system i want the user to be able to login from the index.php page. I have set that up but i am having some problems with getting it to change when the user is logged in. I want it so when the user is not logged in the login form shows and when the user is logged in i want a menu with all user settings to show. This is the code i have tried and it didn't work.
<?php
			if ($_SESSION['username']) {
				echo "You are logged in";
			} else {?> 
			
			<form action="login.php" method="POST">
				<p>
					<label for="username">Username:</label>
					<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
				</p>
				<p>
					<label for="password">Password:</label>
					<input type="password" name="password" id="password" />
				</p>
				<p>
					<input type="submit" name="submit" value="Login" />
				</p>
			</form>
			<?php
			}
			?>
Can someone tell me what i have done wrong here.
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: If statment to say if the user is logged in or not.

Post by Temor »

wignall6 wrote:Hello,

On the user login system i want the user to be able to login from the index.php page. I have set that up but i am having some problems with getting it to change when the user is logged in. I want it so when the user is not logged in the login form shows and when the user is logged in i want a menu with all user settings to show. This is the code i have tried and it didn't work.
<?php
			if ($_SESSION['username']) {
				echo "You are logged in";
			} else {?> 
			
			<form action="login.php" method="POST">
				<p>
					<label for="username">Username:</label>
					<input type="text" name="username" id="username" value="<?php if (isset($_POST['username'])) echo htmlentities($_POST['username']); ?>" />
				</p>
				<p>
					<label for="password">Password:</label>
					<input type="password" name="password" id="password" />
				</p>
				<p>
					<input type="submit" name="submit" value="Login" />
				</p>
			</form>
			<?php
			}
			?>
Can someone tell me what i have done wrong here.
Try checking if the $_SESSION['username'] variable is empty by using the empty() function.

This is the function I use to check if a user is logged in:
function logged_in(){
	if(empty($_SESSION['username'])){ return false; }else{ return true; }
}
if(logged_in() === true){
Do stuff}else{
Do something else}
wignall6
Posts: 38
Joined: Thu May 05, 2011 8:05 pm

Re: If statment to say if the user is logged in or not.

Post by wignall6 »

I was looking through my code and i noticed that i didn't include the include file, that's why my code wasn't working.
Post Reply