用户不通过会话变量显示

时间:2014-06-30 07:33:36

标签: php html login

我是PHP的新手,我建立了一个主要从差异源复制的网站,我希望显示登录的用户但不会。这是我的页面HTML

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

和checklogin.php是

<?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_start();
$_SESSION['login'] = true;
$_SESSION['username'] = $username;
header("location:user_index.html");

}
else {

echo "Wrong Username or Password";
}
?>

4 个答案:

答案 0 :(得分:2)

你忘记了session_start();在checklogin.php文件中..

答案 1 :(得分:1)

您需要在checklogin.php中包含session_start()

<?php
  session_start();
  $host="localhost"; // Host name 
  $username="root"; // Mysql username 
  $password=""; // Mysql password 
  //etc etc

另外......你在这里重定向到一个HTML页面......

header("location:user_index.html");

不应该进入PHP页面吗?

只需将user_index.html另存为user_index.php即可按计划运行

答案 2 :(得分:1)

下面

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

我可以看到你正在将用户重定向到HTML页面

以上你说&#34;这是我的页面的HTML&#34;

所以我在这里理解的是你试图在HTML页面上执行完全不可能的PHP代码。

解决方案:

复制"user_index.html"中的所有代码并将其粘贴到".php"文件中,例如"user_index.php"。 然后尝试一下。

答案 3 :(得分:0)

你的checklogin.php问题中的

是你没有开始会话

session_start();