所以在网上看了一段时间,并试图按照例子......
我想要做的是让用户登录,然后启动我已经测试的会话并且它确实有效,但是我无法从数据库中获取用户名或任何其他数据项。
用户使用用户名和密码登录。然后使用此代码将它们定向到另一页...
<?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']
?>
它是如何显示名称的,我不知道如果这是一个简单的解决方法,是否有人可以帮助我?
-------------------------------------------- UPDATE-- -------------------------------------
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>
checklogin.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";
}
?>
login_success,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');
?>
profile.php
<?php
session_start();
echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']
?>
谢谢James
答案 0 :(得分:2)
在您的代码中,在第一行包含一个Session_start。 如果您的代码使用会话,则必须使用Session_start。它的作用是,如果现有会话存在,则恢复它,或者如果没有会话已经存在,则开始新的会话。
<?php
session_start();
echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name'];
?>
答案 1 :(得分:1)
您也可以在下一页开始会话 使用
session_start();
echo "Pageviews=". $_SESSION['views'];
echo "Welcome=" . $_SESSION['first_name']
答案 2 :(得分: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
&GT;
因为我认为你使用的是升级版本的php所以可能还有一个原因:
session_is_registered(user_name)
自PHP 5.3.0开始,此函数已被弃用,自PHP 5.4.0起已被删除。
来源:http://php.net/manual/en/function.session-is-registered.php