<?php
$con = mysql_connect('localhost','root') or die(mysql_error());
mysql_select_db('good',$con) or die(mysql_error());
session_start();
//setting variables
$user = $_POST['user'];
$pass = $_POST['pass'];
$id = mysql_query("SELECT id FROM users WHERE user='$user'");
if(isset($_POST['submit'])){
$res = mysql_query("SELECT * FROM users WHERE user='$user' AND pass='$pass'");
if(mysql_num_rows($res) > 0){
$mem = mysql_fetch_assoc($res);
$_SESSION['uid'] = $id;
echo "You have succesfully logged in as *insert username here*!";
} else {
echo "The supplied credentials were invalid!";
}
} elseif(isset($_SESSION['uid'])){
echo "You are already logged in as *insert username here*!";
}
?>
First things first, correct any of my mistakes if you can. But what I really need to know is how to select the username of the user who has just logged in based on the session id.
CREATE TABLE IF NOT EXISTS `users` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`user` varchar(40) NOT NULL,
`pass` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
Any advice is good.
Thanks, Edwin =]