我是PHP世界的新手,已经研究了几天了,但似乎被卡住了。
我试图设置一个小网站作为客户帐户区域与MySQL数据库的后端。
我有一个网页的登录表单,用户输入他们的用户名和密码。有一个php脚本,检查SQL数据库,看看是否有匹配,如果有重定向到帐户页面。我在网上找到了一个示例脚本,我用来创建这个,这部分工作得很好。
我想把事情做得更远一点,并在网页上显示匹配的SQL记录的数据。从我所做的研究来看,最好的方法是将会话变量传递给帐户页面。
这是我设置的
第1页- index.php这是一个带有表单的登录页面,其中包含用户名和密码条目。提交时,它运行checklogin.php
Page 2 - checklogin.php
<?php
session_start();
$host="localhost:3306"; // Host name
$username="**********"; // Mysql username
$password="**********"; // Mysql password
$db_name="galactek_myecl"; // Database name
$tbl_name="clients"; // 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");
// Define $myusername and $mypassword
$myusername=$_POST['username'];
$mypassword=$_POST['password'];
// 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);
// set variables
$_SESSION['office_name'] = mysql_query("SELECT office FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");
// 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){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("password");
header("location:myecl.php");
}
else {
header( 'Location: http://www.galactek.com/support/offmaint.html' );
}
?>
第3页- myecl.php这是我想要显示该数据的页面。目前我试图只显示办公室名称,但它一直出现为0。如果我将办公室名称硬编码到checklogin.php页面上的变量中,则不会遇到任何问题。
<?php
session_start();
//if(!session_is_registered(myusername)){
// header("location:index.php");
// }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="en-us" http-equiv="Content-Language" />
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>
<body>
<p>You're HERE!</p>
<?php
echo $_SESSION['office_name']
?>
</body>
</html>
我很确定我忽略了什么。我没有接触过太多PHP,因此无法区分问题是什么。
我认为问题在这里:
$_SESSION['office_name'] = mysql_query("SELECT office FROM $tbl_name WHERE username='$myusername' and password='$mypassword'");
函数mysql_query
不返回字符串,而是返回一个包含查询结果的对象。(请参阅文档。)但实际上,您已经在前一个查询的结果中获得了office,因此不需要再执行另一个查询。您可以使用第一个查询中的变量$result
:
$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){
// Register $myusername, $mypassword and redirect to file "login_success.php"
$row = mysql_fetch_assoc($result); //Get an array with the results.
$office = $row["office"]; //Get the office.
session_register($office); //Register the office with the session.
session_register("username");
session_register("password");
header("location:myecl.php");
}
进一步说明:
首先,应避免使用mysql_
函数。它们已被弃用并将被删除。最重要的是,它们容易出现安全问题。建议使用PDO或MySQLi代替。这是一个很好的mysql初学者指南。
第二,session_register("username");
不注册变量$username
,而是注册字面值字符串"username"
。我不确定那是不是你想做的。
第三,关于使用session_register
,文档有如下说明:
警告:此函数已在PHP 5.3.0中弃用,并在PHP 5.4.0中删除。
考虑使用这样的代码:
$_SESSION['office'] = $row["office"];
是否可以像myecl.php中的echo语句一样简单,需要分号?