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
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
}
?>

Link to comment
Share on other sites

This is what I was looking for and worked as far as registering and logging in are concerned, but after login, instead of showing that I'm logged in, it still shows the Username and password fields along with the options to login and register.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.