Jump to content

You are logged in as...


Isom

Recommended Posts

Alright, so I'm fairly new to PHP coding and I still have a ton to learn, so it's not surprising that I ran into a problem pretty quickly.

I've setup a database and even managed to scrap together a SIMPLE member management system. All of it works, but I still need one thing. A lot of sites I visit which allow users to signup have this at the top; Login or Register. Nothing huge, just in the corner, know what I mean? I was wondering how I do this? Also, after someone logs in, how do I change that to show "You are logged in as [username] and then a logout option?

Link to comment
https://forums.phpfreaks.com/topic/249368-you-are-logged-in-as/
Share on other sites

Use sessions..

Im too lazy to come with something fancy so

 

<?php
session_start();

if(isset($_POST['Login'])){
	$username = $_POST['username'];
	// if you put your password as md5 in your DB
	$password = md5($_POST['password'];
	$sql = "SELECT * FROM tableUsers WHERE userName = '" . $username . "' AND password = '" . $password . "'";
	$result = mysql_query($result);
	$row = mysql_fetch_array($result);
	$_SESSION['id'] = $row['id'];
	$_SESSION['username'] = $row['userName'];
}

if($_GET['action'] == 'logout'){
	session_unset();
}
// if session is set, show the username
if(isset($_SESSION['id'])){
	echo "You are logged in as " . $_SESSION['username'] . "<br /><br />";
	echo "<a href='login.php?action=logout'>Logout</a>";
}else{
        // else you show login or register
?>
<a href="login.php">Login</a> or <a href="register.php">Register</a>

<form name="loginForm" method="post" action="login.php">
	<input type="textbox" name="username" />
	<input type="password" name="password" />
	<input type="submit" name="Login" value="Login" />
</form>
<?php
}
?>

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.