Please can you help, i am new to php but starting to get a bit of grip but sessions are causing me a problem, i have been stuck on this for a while now.
I have a basic html / php log in form, the form should put the username from the form into a session and then the rest of the php scripts use this session data for various sql queries.
My problem is that the login username will only go into the session on the second attempt, the first attempt results in nothing going into the session and a blank return? but if i go back and repeat the log the data goes into the session fine?
I have been looking in the answers already posted and the most relevant told me to add the full web address in the redirect url but this had no effect.
Please can someone help me as this is driving me mad!!
Here is the html form:
<table width="300" border="0" align="center" cellpadding="0" cellspacing="1"
bgcolor="#CCCCCC">
<tr>
<form name="form1" method="post" action="checkLogIn.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1"
bgcolor="#FFFFFF">
<tr>
<td colspan="3"><div align="center"><strong>Employee Log In</strong></div>
</td>
</tr>
<tr>
<td width="27%">Username</td>
<td width="4%">:</td>
<td width="69%"><input name="myusername" type="text" id="myusername2"></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input name="mypassword" type="password" id="mypassword2"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Login"></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<?php
// Start the session
session_start();
echo "Welcome ".$_SESSION['user']."!";
?>
The PHP script:
<?php
session_start();
$host="db659279157.db.1and1.com"; // Host name
$username="dbo659279157"; // Mysql username
$password="password1"; // Mysql password
$db_name="db659279157"; // 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");
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_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 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){
$_SESSION['user'] = $myusername;
echo "Welcome ".$_SESSION['user']."!";
echo "<meta http-equiv=\"refresh\" content=\"0;URL=http://www.morgan-
data.co.uk/logInHome.php\">";
} else {
echo "<p>Wrong Username Or Password, Please Press Back To Try Again</p>";
}
?>
The page the user is directed to with successful log in:
<body>
<div align="center">
<table>
<tr>
<td><div align="center"><a href="../php/insert2.php"><img
src="../graphics/timesheet.jpg" width="225" height="225" border="0"></a>
</div></td>
<td><div align="center"><a href="view.php"><img
src="../graphics/planner.jpg" width="240" height="175" border="0"></a></div>
</td>
<td><div align="center"><a href="storeFinder.htm"><img
src="../graphics/location.gif" width="240" height="175" border="0"></a>
</div></td>
</tr>
<tr>
<td><div align="center">Submitt Timesheet</div></td>
<td><div align="center">View Planner</div></td>
<td><div align="center">Find Store Address</div></td>
</tr>
</table>
</div>
<?php
echo "Welcome ".$_SESSION['user']."!";
?>