0

所以一直在网上找了一段时间,并试图按照例子......

我想做的是让用户登录,然后开始一个我已经测试过的会话,它确实有效,但是我无法从数据库中获取用户名或任何其他数据项。

用户使用用户名和密码登录。然后,他们将被引导到具有此代码的另一个页面...

<?php

session_start();
if(!session_is_registered(user_name));

$con = mysql_connect("****","****","****");
            if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }
       //Finds database
            mysql_select_db("****", $con);

$result = mysql_query("SELECT * FROM fyp_users;");


$first_name = $_SESSION['first_name'];
$_SESSION['views']=200;
header('location:../profile.php');


?>

会话视图只是一个测试,它可以在用户登录时查找并显示数字。然后我尝试进行 first_name 会话。

这也是它然后重定向的页面......

<?php 
$con = mysql_connect("****","****","****");
            if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }
       //Finds database
            mysql_select_db("****", $con);
?>
<?php
session_start( );

//$result = mysql_query("SELECT * FROM fyp_users;");

?>  

一些 HTML....

<?php

echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['$first_name']


?>

它如何不显示名称,我不知道它是否容易修复,有人可以帮助我吗?

- - - - - - - - - - - - - - - - - - - - - - 更新 - - - ----------------------------------

HTML 表格

<div class="login">
<form id="login" method="post" action="includes/checklogin.php">
<p>Login</p>
<p>Username</p>
<input name="user_name" id="user_name">
<p>Password</p>
<input name="password" id="password" type="password">
</br>
<input type="submit" name="Submit" value="Login">
</form>
</div>
<!--<img src="images/logo.png" alt="The Community">-->
</div>

检查登录.php

<?php

$con = mysql_connect("****","***","***");
            if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }
       //Finds database
            mysql_select_db("****", $con);

$user_name=$_POST['user_name']; 
$password=$_POST['password']; 

$user_name = stripslashes($user_name);
$password = stripslashes($password);
$user_name = mysql_real_escape_string($user_name);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM fyp_users WHERE user_name='{$user_name}' and       password='{$password}'";
$result=mysql_query($sql);



$count=mysql_num_rows($result);

if($count==1) {

session_register("user_name");
session_register("password"); 
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
?>

登录成功,php

<?php

session_start();
if(!session_is_registered(user_name));

$con = mysql_connect("***","****","****");
            if (!$con)
              {
              die('Could not connect: ' . mysql_error());
              }
       //Finds database
            mysql_select_db("*****", $con);

$result = mysql_query("SELECT * FROM fyp_users;");


$first_name = $_SESSION['first_name'];
$password = $_SESSION['password'];
$_SESSION['views']=200;
header('location:../profile.php');


?>

配置文件.php

<?php
session_start();


echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']


?>

谢谢詹姆斯

4

3 回答 3

3

在您的代码中,在第一行包含一个 Session_start。如果您的代码使用会话,则 Session_start 是必须的。它的作用是,如果存在会话,则恢复现有会话,或者如果不存在会话,则开始新的新会话。

<?php
session_start();


echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name'];


?>
于 2013-08-21T23:14:48.703 回答
1

你已经在下一页开始了会话也使用

session_start(); 

echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']
于 2013-08-21T23:15:48.527 回答
0
<?php
session_start(); //use this to invoke session variable usage

echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']


?>



if(!session_is_registered(user_name)); // this statement seems incorrect

应该是

<?php

session_start();
if(!session_is_registered(user_name)) : // it should be colon with endif at end
// session_is_registered('user_name')  user_name should be within single quotes untill its DEFINED

$con = mysql_connect("****","****","****");
        if (!$con)
          {
          die('Could not connect: ' . mysql_error());
          }
   //Finds database
        mysql_select_db("****", $con);

$result = mysql_query("SELECT * FROM fyp_users;");

$first_name = $_SESSION['first_name'];
$_SESSION['views']=200;
header('location:../profile.php');

endif; // here is end if

?>

因为我认为您正在使用升级版的 php,所以可能还有一个原因:

session_is_registered(user_name)

自 PHP 5.3.0 起,该函数已被弃用,自 PHP 5.4.0 起已移除。

来源: http: //php.net/manual/en/function.session-is-registered.php

于 2013-08-21T23:14:07.730 回答