我认为这不是一个难题,但我陷入了困境。目前我使用php建立了一个登录和注册(注册)界面。我的想法很直接:index.php显示“登录”和“注册”,如果您在数据库中没有帐户,您将被引导到“注册”页面,填写完所有需要的信息后,我想将用户引导至显示“您已注册为”的页面。问题出在这里,我尝试使用 $_SESSION['username'] 来对应用户是谁。但不幸的是,没有打印出用户名。我在registered.php 中使用了isset() 来检查是否设置了$_SESSION['username']。它表明它没有设置,但它应该在那里!
流程是signup.php -> register.php -> registered.php。我的问题是 $_SESSION['username'] 应该在声明 session_start() 之后在 php 文件中传递。下面是我的代码,有人会指出我哪里出错了。
非常感谢。
注册.php
<?php
session_start(); // attention add session_start() again, am I wrong?
$title = "Sign up";
print_r($_SESSION);
?>
<html>
<head>
<title><?= $title ?></title>
<!--
<link href="index.css" type="text/css" rel="stylesheet"></link>
-->
<link href="signup.css" type="text/css" rel="stylesheet"></link>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<div id="main-content">
<form method="post" action="register.php">
<label><input class="defaultText" title="username" name="username" type="text"></label><br/>
<label><input class="defaultText" title="email" name="email" type="text"></label><br/>
<label><input class="defaultText" title="company" name="company" type="text"></label><br/>
<label class = "password">password:<input name="pass1" type="password" /></label><br/>
<label class = "password">password (again):<input name="pass2" type="password"/></label><br/>
<input type="submit" value="Sign up!">
</form>
<div style="clear: both;"></div>
</div>
</body>
</html>
注册.php
<?php
if(isset($_REQUEST['username']) && isset($_REQUEST['email']) && isset($_REQUEST['company']) && isset($_REQUEST['pass1']) && isset($_REQUEST['pass2'])){
$username = $_REQUEST['username'];
$email = $_REQUEST['email'];
$company = $_REQUEST['company'];
$pass1 = $_REQUEST['pass1'];
$pass2 = $_REQUEST['pass2'];
if(strlen($username) <= 0) {
die("Username must be greater than 0 characters\n");
}
if(strlen($email) <= 0) {
die("Email must be greater than 0 characters\n");
}
if(strlen($company) <= 0) {
die("Company name must be greater than 0 characters\n");
}
if($pass1 != $pass2 && $pass1 <= 0) {
die("Passwords are not the same.");
}
$hash = hash('sha256', $pass1);
# connect to world database on local computer
check(mysql_connect("localhost", "root", "123456"), "connect");
$connect = mysql_connect("localhost", "root", "123456");
# in order to insert data into database tables, database is already there built via mySQL command from terminal
# connect with the database
check(mysql_select_db("product"), "selecting db");
# mysql_real_escape_string() This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.
$username = mysql_real_escape_string($username);
$email = mysql_real_escape_string($email);
$company = mysql_real_escape_string($company);
# this $query query is to send the "insert values" query into a table
$query = "INSERT INTO admins ( username, hash, email, company ) VALUES ( '$username', '$hash', '$email', '$company' );";
$results = mysql_query($query);
check($results, "adding user"); // if not pass show error message
header("Location: registered.php");
}
function check($result, $message) {
if (!$result) {
die("SQL error in $message: " . mysql_error());
}
$_SESSION['username'] = $_REQUEST['username'];
$_SESSION['email'] = $_REQUEST['email'];
$_SESSION['company'] = $_REQUEST['company'];
}
?>
注册.php
<?php
$title = "Registered!";
?>
<html>
<head>
<title><?= $title ?></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="index.js"></script>
</head>
<body>
<?php
print_r($_SESSION);
if(isset($_SESSION['username']))
print "hello";
print $_SESSION['username'];
echo $_SESSION['company'];
?>
<div id="main-content">
<h3>You have registered in administration system as <?= $_SESSION['username'] ?></h3>
<table border="1">
<tr>
<th>Name</th>
<th>Company</th>
<th>Email</th>
</tr>
<tr>
<td><?= $_SESSION['username'] ?></td>
<td><?= $_SESSION['email'] ?></td>
<td><?= $_SESSION['company'] ?></td>
</tr>
<h3>please log in into the administration system</h3>
<h4><a href = "index.php">Log In</a></h4>
</table>
<div style="clear: both;"></div>
</div>
</body>
</html>