NEWBE: Save user info to display in member area. Cookie?
Posted: Wed Mar 19, 2008 3:23 pm
Here's what i have so far, all it does is display jonny 4, but i want it to get the users name from the form and display it as "welcome 'username' on account.php page when the user logs in.
login.php
This displays the username on the account.php page
Thanks for the help in advance.
I know proboubly the better way in doing this is to do through sessions but i'm just not php savy enough "yet". Maybe point me in the right direction?
login.php
Code: Select all
<?php
session_start();
$username = 'jonny4';
setcookie('username', $username);
$user_area_location = 'account.php'; // Location of the user area
// Connect to MySQL database:
$username="";
$password="";
$database="";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$error = array();
if($_GET['action']) {
switch($_GET['action']) {
case 'logoff':
unset($_SESSION['loggedIn']);
array_push($error, 'You were logged off.');
break;
}
}
if(!$error) {
if(empty($_POST['username'])) { array_push($error, 'You didn\'t supply a username'); }
if(empty($_POST['password'])) { array_push($error, 'You didn\'t supply a password'); }
}
if(!$error){
$result = @mysql_query('SELECT name, email FROM `users` WHERE username = \''.mysql_real_escape_string($_POST['username']).'\' AND password = \''.mysql_real_escape_string(md5($_POST['password'])).'\'');
if($row = @mysql_fetch_row($result)) {
$_SESSION['loggedIn'] = true;
header('Location: '.$user_area_location);
die('<a href="'.$user_area_location.'">Go to your user account</a>');
}else{
array_push($error, 'The credentials you provided were not correct');
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
</head>
<body>
<table width="284" height="162" border="0" cellpadding="0" cellspacing="2">
<form method="post" action="login.php">
<?php if(isset($error) && $error) { ?>
<tr>
<td colspan="2"> <ul>
<?php foreach($error as $key => $value) echo '<li>'.$value.'</li>'; ?>
</ul></td>
</tr>
<?php } ?>
<tr>
<td>Username:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Login!" /></td>
</tr>
<tr>
<td colspan="2">Not a User? <a href="register.php">Register Here</a></td>
</tr>
</form>
</table>
</body>
</html>
Code: Select all
echo $_COOKIE['username'];
I know proboubly the better way in doing this is to do through sessions but i'm just not php savy enough "yet". Maybe point me in the right direction?