So I have some decent history with HTML/CSS but only recently started to look into PHP. I understand the logic behind it, but actually making something happen is still a bit beyond me. I borrowed a nice login script from the internet and it has been working wonderfully for me. The only thing is that I would like to print a person’s name on a page after they have logged in. (i.e. Welcome John Doe!). I have tried a dozen different things and still can’t manage to produce anything of value. I have a database with fields for username, password, and realname.
The script I’m using for login is:
[php]<?php
$host=“placeholder.com”; // Host name
$username=“admin”; // Mysql username
$password=“pw”; // Mysql password
$db_name=“login”; // Database name
$tbl_name=“members”; // 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”);
// username and password sent from form
$myusername=$_POST[‘myusername’];
$mypassword=$_POST[‘mypassword’];
// To protect 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 username=’$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
session_register(“myusername”);
session_register(“mypassword”);
header(“location:memberarea.php”);
}
else {
echo " ";
}
?>[/php]
And on each page to verify login it starts with:
[php]
<?php session_start(); if(!session_is_registered(myusername)){ header("location:login.html"); } ?>[/php]