我正在创建一个简单的SPA
会员注册系统。注册后 - 用户被存储在数据库中 - 并被重定向到个人资料页面。看起来相当简单,但是,在尝试使用密码“joe”和电子邮件“joe@joe.com”注册为“joe”之后,我被重定向到显示我的用户名是“root”的个人资料页面。我已经检查以确保我的 mysqli 连接变量$username
没有与数据存储混在一起(因为它的值是“root”)。这是我的代码:
checkreglogin.php
<?php
session_start();
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="bonjour3"; // Mysql password
$db_name="itit"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
$mysqli = new mysqli("$host", "$username", "$password", "$db_name")or die("cannot connect");
// Define $myusername and $mypassword
$rmyusername=$_POST['rmyusername'];
$rmypassword=$_POST['rmypassword'];
$myemail=$_POST['myemail'];
$sql2 = $mysqli->prepare("SELECT * FROM $tbl_name WHERE username=? and password=?");
$sql2->bind_param('ss',$rmyusername,$rmypassword);
$sql2->execute();
$sql2->store_result();//apply to prepare statement
$numRows = $sql2->num_rows;
if($numRows != null) {
echo "Username taken.";
session_destroy();
}
else {
// Insert data into mysql
$sql = "INSERT INTO $tbl_name(username, password, email)VALUES(?,?,?)";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param("sss", $rmyusername, $rmypassword, $myemail);
$stmt->execute();
$_SESSION['username'] = $rmyusername;
}
?>
上面的脚本在用户注册时运行。然后他们被“重定向”(不是真的因为它是一个 SPA)到下面的个人资料页面:
模板/login_success.php
<?php
session_start();
?>
<h1>Login Successful</h1>
<h2>Username: <? echo $_SESSION['username']?></h2>
<div id="logoutlinkdiv" >
<a href = "#" >Log out</a>
</div>
用户名应该在它说的地方显示“joe” <? echo $_SESSION['username']?>
,但是它说“root”而不是......
更新
js/application.js
$(document).ready(function() {
$.get('templates/regform.php', function(data) {
$('#register').html(data);
$("#registerform").submit(function(e) {
e.preventDefault();
if ($('#rmyusername').val() != '' || $('#rmypassword').val() != '' || $('#myemail').val() != '') {
if(!isValidEmailAddress($('#myemail').val())) {
alert("Please enter a valid email address");
}
else {
$.post("checkreglogin.php", $(this).serialize(), function(){
$("#main").load("templates/login_success.php");
$("#login").remove();
$("#register").remove();
});
}
} else {
alert('Please enter a Username/Password and email.');
}
});
});
});
更新
我将$_POST
调用更改为$_GET
调用,并使用 url 创建用户,并且它有效 - 但是我得到一个带有警告的空白页面:
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /Users/Eamon/Sites/checkreglogin.php:1) in /Users/Eamon/Sites/checkreglogin.php on line 3