i am new to the PHP and i building a website which is mostly copied from diff sources and i want the user who had logged in to be displayed but it does not. here is my HTML for the page

<?PHP

session_start();

if (!(isset($_SESSION['username']) && $_SESSION['username'] != '')) {

header ("Location: checklogin.php");

}

?>

<!DOCTYPE html>
<html>
 <head>

<title>MAP MY WAY</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

<meta name="description" content="Google Maps API V3: Custom Directions Panel" />
<meta name="keywords" content="final_project" />
<meta name="author" content="Zohaib Tasnim" />

 <script type="text/javascript"   
 src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
 <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?      
 v=3.exp&sensor=true&libraries=places"></script>
<link href="style.css" type="text/css" rel="stylesheet" />


<!--to disable the enter key-->
<script type="text/javascript"> 

 function stopRKey(evt) { 
 var evt = (evt) ? evt : ((event) ? event : null); 
 var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
 if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
 } 

 document.onkeypress = stopRKey; 

 </script>


 </head>
 <body>
 <div id="mapCanvas">&#160;</div>
 <div id="box">
    <a id="btns"> welcome <?php echo($_SESSION['username']); ?></a>
    <a id="btns" href="profile.html"> My Profile</a>
    <a id="btns" href="login.php"> logout</a>
    <p class="or">[OR]</p>
    <div class="directionInputs">
        <form action="address_insert.php" name="form2" method="post">
            <p><label>A</label><input type="text" value=""  name="loc_a" 
  id="dirSource" /></p>
            <p><label>B</label><input type="text" value="" name="loc_b" 
  id="dirDestination" /></p>
            <a href="#getDirections" id="getDirections">Get Directions</a>
            <a href="#reset" id="paneReset">Reset</a>
            <a id="btns"><input name="submit" class="btns" type="submit" value="Save       
  Address" /></a>
        </form>  
    </div>

    <div id="directionSteps">
        <p class="msg">Direction Steps Will Render Here</p>
    </div>
    <!--<a href="#toggleBtn" id="paneToggle" class="out">&lt;</a>-->
</div>
<script type="text/javascript" src="sample.js"></script> 
</body>
</html>

and checklogin.php is

<?php

 $host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="map_my_way"; // 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 
$username=$_POST['username']; 
$password=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password='$password'";
$result=mysql_query($sql);

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

// If result matched $username and $password, table row must be 1 row
if($count==1){

// Register $username
$_SESSION['login'] = true;
$_SESSION['username'] = $username;
header("location:user_index.html");

}
else {

echo "Wrong Username or Password";
}
?>

You need session_start() at the top of all scripts that intend to use the session state, whether to read from or set it.
Adding session_start() to your checklogin.php should fix the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.