-2

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>&nbsp;</td>
        <td>&nbsp;</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']."!";

?>

  • 2
    I sure hope those mysql credentials arn't real – IsThisJavascript Jan 05 '18 at 13:01
  • MySQL functions are deprecated and removed since php 7.0 please switch to using [mysqli](http://php.net/manual/en/mysqli.quickstart.php) or [pdo](http://php.net/manual/en/book.pdo.php) – IsThisJavascript Jan 05 '18 at 13:02
  • **Never store plain text passwords!** Please use ***PHP's [built-in functions](http://jayblanchard.net/proper_password_hashing_with_PHP.html)*** to handle password security. If you're using a PHP version less than 5.5 you can use the `password_hash()` [compatibility pack](https://github.com/ircmaxell/password_compat). ***It is not necessary to [escape passwords](http://stackoverflow.com/q/36628418/1011527)*** or use any other cleansing mechanism on them before hashing. Doing so *changes* the password and causes unnecessary additional coding. – Jay Blanchard Jan 05 '18 at 13:04
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)***. Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jan 05 '18 at 13:04

1 Answers1

0

Put the session_start(); at the beginning of the HTML document like this:

<?php
    session_start();
?>
<!DOCTYPE ....
Ivan86
  • 5,695
  • 2
  • 14
  • 30