Page 1 of 1

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

Posted: Wed Jun 29, 2011 3:38 pm
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.

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

Posted: Wed Jun 29, 2011 4:22 pm
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}

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

Posted: Wed Jun 29, 2011 4:39 pm
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.