Ok, this login script I've been working on finally works with cookies. But, now I want to make it work with a Session. I've never done a session before. But, I read a tutorial and tried my best to make it work. I have the problem Creating the Session and then seeing if they are logged in.
PHP Code:
<?php
if ($_POST[user] && $_POST[pass]) {
$link = mysql_connect ("localhost","username","password");
$database = mysql_select_db("database_name");
//$sql = "SELECT * FROM users WHERE username=".$_POST['user'];
$sql = "select id, username, password from users where username='$_POST[user]' and password='$_POST[pass]'";
$result = mysql_query($sql);
if (mysql_error()) {
// print(mysql_error());
$login_error= true;
}
else { // There was no error
if ($data = mysql_fetch_array($result)) {
if ($data[id] > 0) { // I HAVE TROUBLE STARTING HERE
session_start();
header("Cache-control: private"); //IE 6 Fix
$_SESSION['user'] = $_POST['user'];
mysql_close($link);
header("Location: test.php");
}
else { $login_error= true; }
}
else {
$login_error= true;
}
}
}//End Of Submit Request
if ($login_error== true){
print("Login Error Page. Go Back.<br />");
}
else {
if($_SESSION['user']){
?>
You Are logged in as <?php $_SESSION['user']; ?>
<a href=test.php?logout=true><u>Logout</u></a>
<?php
}
else {
?>
<form action=test.php method=post name="login" style="margin:0;">
Username<br /><input type="text" name="user" size="15" maxlength="20" class="textinput">
<br />
Password<br /><input type="password" name="pass" size="15" maxlength="25" class="textinput">
<br />
<input type="submit" name="submit" value="Submit" class="submit">
</form>
<?php
}
}
?>
Comment