1
votes

I have adpopted some code to create a login, checklogin, successful login, failed login and logout pages. The checklogin page essentially checks the username and password posted from login. If these are correct you end up at successful login page. However I want to say something like 'Welcome John' on the successful login page, but dont know how to get the username from the session so that I can base a query on this to pull back the logged in persons name. The checklogin page is:

<?php require_once('Connections/Connection1.php'); ?>

<?php 
//$host="localhost"; // Host name 
//$username=""; // Mysql username 
//$password=""; // Mysql password 
//$db_name=""; // Database name 
$tbl_name="users"; // Table name 



// Connect to server and select databse.
//mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
//mysql_select_db("$db_name")or die("cannot select DB");
mysql_select_db($database_Connection1, $Connection1);

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=md5($_POST['mypassword']); 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE userid='$myusername' and password='$mypassword'";
$result=mysql_query($sql);



// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
header("location:main.php");
}
else {
header("location:login_failed.php");
//echo "Wrong Username or Password";
}
?>

Then on my successful login page, how do I echo the username? or at least refer to it in a query to then pull back the additional information like name?

Many thanks!!

3
see those tutorials, it could help you thenewboston.org/watch.php?cat=11&number=136 - Ligth
I believe you need session_start(); in all pages, then use an if (isset...$_POST) and echo $myusername. Something along those lines. - Funk Forty Niner
What happens if my username is "' or 1=1;delete from users;--" ? - Ed Manet
@Ed Manet sh*t happens then :) heh Secure your code against SQL Injections - Robert

3 Answers

2
votes

When I need to do this, I usually use php's $_SESSION array and set quick look up information in there, such as a name that might be displayed on multiple pages.

 $_SESSION['username'] = $username;
 $_SESSION['firstname'] = $firstname;
 $_SESSION['lastname'] = $lastname;
 $_SESSION['user_id'] = $id;

This gives me access to all these variables on any page that calls session_start() at the start of the php page.

On a sidenote - using MD5 hashing passwords IS NOT SECURE! Learn how to use crypt() function with salts. It will be good practice and make your database much more secure. Unfortunately MD5 is not much more secure than plaintext at this point with people having easy access to rainbow tables and programs like ocl-hashcat.

Please look into using PDO or mysqli for your database calls. mysql_ functions are deprecated.

1
votes

Changed the code a bit, this is safer..

<?php
session_start();
// Sanitize $_POST['myusername'] and $_POST['mypassword'] before loading into session variables to protect from MySQL injection
$_SESSION["myusername"]=!empty($_POST['myusername'])?mysql_real_escape_string(stripslashes($_POST['myusername'])):"";
$_SESSION["mypassword"]=!empty($_POST['mypassword'])?mysql_real_escape_string(stripslashes($_POST['mypassword'])):"";

// Load database variables, connect to server and select a database
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
mysql_connect($host, $username, $password)or die("Cannot Connect for Reason:".mysql_error());
mysql_select_db("$db_name")or die("Cannot Select DB for Reason:".mysql_error());

// Run query
$result=mysql_query("SELECT username FROM $tbl_name WHERE username='".$_SESSION["myusername"]."' AND password='".$_SESSION["mypassword"]."'");

// Check for return of single record and direct to login_success.php
if(mysql_num_rows($result)==1){header("location:login_success.php");}
else{
// On login falier, unset session variables if not needed and redirect
unset($_SESSION["myusername"]); // Optional if return value not needed or wanted
unset($_SESSION["mypassword"]); // Optional if return value not needed or wanted
header('refresh: 5; url=./login_fail.php');
die("Wrong Username or Password. Redirecting..."); // To prevent evil people manipulating the page, kill the script using die.
}
?>

To echo the user on another page: echo $_SESSION['myusername'];

0
votes

You have two options...

Either you can retrieve it on main.php page as $_SESSION['myusername'];

Or, you can use pass it to main.php as header("location:main.php?username=".$myusername);

And then on main.php, you can retrieve it as $_GET['username'];