SnippetsProject Code

Check If User Is Logged In

Description

This Snippet will check and see if if a user is logged in or not.

Function

functions.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
function loggedIn(){
	//Session logged is set if the user is logged in
	//set it on 1 if the user has successfully logged in
	//if it wasn't set create a login form
	if(!$_SESSION['loggd']){
		echo'<form action="checkLogin.php" method="post">
		<p>
			Username:<br>
			<input type="text" name="username">
		</p>
		<p>
			Password:<br>
			<input type="password" name="username">
		</p>
		<p>
			<input type="submit" name="submit" value="Log In">
		</p>
		</form>';
		//if session is equal to 1, display 
		//Welcome, and whaterver their user name is
	}else{
		echo 'Welcome, '.$_SESSION['username'];
	}
}
?>

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
//Start the session
session_start();

//This is a simplified HTML Document
?>
<html>
<head>
<title>My Page</title>
</head>
<body>
	<?php
	//Call the functions file
	require_once("functions.php");
	//Display either the user's name, or the login form
	//This can be placed on many pages without having
	//to re-write the form everytime, just use this function
	logedIn();
	?>
</body>
</html>

Comments

ADD YOUR COMMENT:

Formatting

Comments.
Search Engine Optimization August 28th 2008 (Website)
I'm still learning php so any information I find is great. Thanks for the great post.
pedantic arse August 27th 2008
its logged not loged

Login



Did you want to add your own codes?
Create an Account its fast and easy!

Did you forget your username/password?
Have us reset it it for you!

Information

2.85 / 5 (301 votes)

8,234 views

3 downloads New

Code Tools

Bookmark and Share

Programmer

admin

Posted on: Jan 01, 2008

View all of Ryan's snips