Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
HI I'm trying make a Diary application where people can log in and submit to their daily diary. I have created. I have started to create the login page and once the user is logged in he will be directed to a members page.
What Im having a hard time with is getting the users name to display on the members page. Im not sure how to get the user name. I have been doing the php tutorials. here's what I have so far and hope some of you guys can point me to the right directions.

my login.php
<?php
include 'conn.php';

if (loggedin()==TRUE)
	header("Location: usersarea.php");

if ($_POST['login'])
{
	//get data
	$username = $_POST['username'];
	$password = $_POST['password'];
	$rememberme = $_POST['rememberme'];
	// $login = $_POST['login'];
	
	
	if ($username&&$password)
	{
	
	$login = mysql_query("SELECT * FROM users WHERE username='$username'");
	while ($row = mysql_fetch_assoc($login))
	{
		$db_password = $row['password'];
		if(md5($password)==$db_password)
		$loginok = TRUE;
		else
		$loginok = FALSE;
		if ($login==TRUE)
		{
			if($rememberme== 0)
			setcookie("username", $username, time()+7200);
			else if($rememberme== 1 )
			$_SESSION['username']=$username;
			
			header("Location: usersarea.php");
			exit();
			
		}
		else
			echo("incorrect username/password.");
		
	}
		
	}
	else{
		echo("Wrong");
	}
}

?>

<form action="login.php" method="POST" accept-charset="utf-8">
	<label for="username">username</label><br />
	<input type="text" name="username" value="" id="username">
	<br />
	<label for="password">passord</label><br />
	<br />
	<input type="text" name="password" value="" id="password">
	<br />
<label for="rememberme">rememberme</label><br />
<input type="checkbox" name="rememberme" value="" id="rememberme">

	<p><input type="submit" name="login" value="login"></p>
</form>



userarea.php
<?php
include 'conn.php';

if(!loggedin())
{
	header("Location: login.php");
	exit();
}


?>
<?php
<p>$_SESSION['username']=$username;</p> 

?>
<p>You're logged in!</p>
	
<a href="logout.php">Logout</a>

<pre lang="text">


conn.php
<?php

//session_start
session_start();

//connect to my database
mysql_connect("127.0.0.1","root","") or die();
mysql_select_db("users") or die();

//login check functions
function loggedin()
{
	if(isset($_SESSION['username'])||isset($_COOKIE['username']))
	{
		$loggedin = TRUE;
		return $loggedin;
	}
}



?>




my db
<pre lang="text">
CREATE TABLE `users` (
  `id` int(25) NOT NULL AUTO_INCREMENT,
  `username` varchar(65) NOT NULL DEFAULT '',
  `password` varchar(32) NOT NULL DEFAULT '',
  `EmailAddress` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
Posted

1 solution

First read about session in php.

In summery, to use session, in every page you will have to call function session_start, you must have to call it before you send any kind of data to output.

to store and retrieve data in session just use
$_SESSION['UserId']=$UserId;

use isset function to find the existence of index. to remove a session variable use unset function.


to close session use session_close function or look up in google
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900