session does not start when user first logs in on on the second attempt : <p>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. </p>

<p>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.</p>

<p>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?</p>

<p>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.</p>

<p>Please can someone help me as this is driving me mad!! </p>

<p>Here is the html form:</p>

<pre><code>&lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1" 
bgcolor="#CCCCCC"&gt;
&lt;tr&gt;
&lt;form name="form1" method="post" action="checkLogIn.php"&gt;
&lt;td&gt;
&lt;table width="100%" border="0" cellpadding="3" cellspacing="1" 
bgcolor="#FFFFFF"&gt;
&lt;tr&gt; 
&lt;td colspan="3"&gt;&lt;div align="center"&gt;&lt;strong&gt;Employee Log In&lt;/strong&gt;&lt;/div&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt; 
&lt;td width="27%"&gt;Username&lt;/td&gt;
&lt;td width="4%"&gt;:&lt;/td&gt;
&lt;td width="69%"&gt;&lt;input name="myusername" type="text" id="myusername2"&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt; 
&lt;td&gt;Password&lt;/td&gt;
        &lt;td&gt;:&lt;/td&gt;
        &lt;td&gt;&lt;input name="mypassword" type="password" id="mypassword2"&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
        &lt;td&gt;&amp;nbsp;&lt;/td&gt;
        &lt;td&gt;&lt;input type="submit" name="Submit" value="Login"&gt;&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
&lt;/td&gt;
&lt;/form&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;?php
// Start the session
session_start();
echo "Welcome ".$_SESSION['user']."!";
?&gt;
</code></pre>

<p>The PHP script:</p>

<pre><code>&lt;?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 "&lt;meta http-equiv=\"refresh\" content=\"0;URL=http://www.morgan-
data.co.uk/logInHome.php\"&gt;";
} else {
echo "&lt;p&gt;Wrong Username Or Password, Please Press Back To Try Again&lt;/p&gt;";
}
?&gt;
</code></pre>

<p>The page the user is directed to with successful log in:</p>

<pre><code>&lt;body&gt;
&lt;div align="center"&gt;
&lt;table&gt;
&lt;tr&gt; 
&lt;td&gt;&lt;div align="center"&gt;&lt;a href="../php/insert2.php"&gt;&lt;img 
src="../graphics/timesheet.jpg" width="225" height="225" border="0"&gt;&lt;/a&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;&lt;a href="view.php"&gt;&lt;img 
src="../graphics/planner.jpg" width="240" height="175" border="0"&gt;&lt;/a&gt;&lt;/div&gt;
&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;&lt;a href="storeFinder.htm"&gt;&lt;img 
src="../graphics/location.gif" width="240" height="175" border="0"&gt;&lt;/a&gt;
&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;div align="center"&gt;Submitt Timesheet&lt;/div&gt;&lt;/td&gt;
&lt;td&gt;&lt;div align="center"&gt;View Planner&lt;/div&gt;&lt;/td&gt;
  &lt;td&gt;&lt;div align="center"&gt;Find Store Address&lt;/div&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;?php

echo  "Welcome ".$_SESSION['user']."!";

?&gt;
</code></pre>

<p></p>
