所以我正在构建一个用户注册的网站,当他登录时,他/她可以做一些事情,例如未标记的引用(添加回),用户未注册的标记(从公共视图中删除)。但是,我在弄清楚为什么我的控制器文件上的标题('location:index.php')不会将页面重定向到index.php页面时遇到问题。我已经尝试注释掉所有echo实例(“在我的控制器中的一些东西“”,看看它是否为什么不起作用,但它也没有用。我没有留下为什么标题('location:index.php')不起作用的线索。
这是我的控制器文件,我在成功注册后重定向用户:
<?php
session_start();
include "DataBaseAdaptor.php";
function GetQuotes($theDBA) {
$array = $theDBA->getQuotesAsArray();
$quotes = [];
//$quotes[] = [];
$i = 0;
$j = 0;
$id = "";
if(count($array)>0){
foreach($array as $item) {
$quotes[$i][$j] = $item['quote'];
$quotes[$i][$j+1] = $item['author'];
$quotes[$i][$j+2] = $item['rating'];
$quotes[$i][$j+3] = $item['id'];
$i++;
}}
return $quotes;
}
$quotes = GetQuotes($theDBA); //This to display the quotes initially
// This part is for registering the user
if(isset($_GET['user']) && isset($_GET['pass'])) {
$username = $_GET['user'];
$password = $_GET['pass'];
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
// If false, then username already exists
if (!$theDBA->register($username, $hashed_password )) {
//echo("Username already exists");
}
else { //The control does come to this block and the else is evaluated to true. However it won't redirect
$_SESSION['user'] = $username;
header('Location: index.php');
}
}
// Register ends here----------------------------------------------------------------------------------
//This part is for adding quote
if(isset($_GET['quote']) && isset($_GET['author'])) {
$quote = $_GET['quote'];
$quote = str_replace("'", "", $quote);
$author = $_GET['author'];
$theDBA->addQuote($quote, $author);
$quotes = GetQuotes($theDBA);
header( 'Location: index.php' );
}
// This is for logging in
$quotes = GetQuotes($theDBA);
$user = "";
$pass = "";
if(isset($_GET['p']) && isset($_GET['q'])) {
$user = isset($_GET['p']);
$pass = isset($_GET['q']);
}
for ($i = 0; $i < count($quotes); $i++){
if($user === $quotes[$i][0]) {
$_SESSION['user'] = 'Rick';
header('Location: index.php');
}
}
echo json_encode($quotes);
?>
我不知道你是否需要我的model.php(在我的情况下是DataBaseAdaptor.php),但这里是。与数据库相关的所有内容都与所有sql操作一样正常。但如果你需要它,请发现它。如果您发现model.php没有必要,请随时向我的帖子发送编辑建议(但请,只有在您确定没有必要时才这样做)。谢谢!
这是model.php
<?php
//
// Author: Rick Mercer. File name: model.php
//
class DatabaseAdaptor {
// The instance variable used in every one of the functions in class
DatbaseAdaptor
private $DB;
// Make a connection to an existing data based named 'first' that has table
customer
public function __construct() {
$db = 'mysql:dbname=quotes; charset=utf8; host=127.0.0.1';
$user = 'root';
$password = '';
try {
$this->DB = new PDO ( $db, $user, $password );
$this->DB->setAttribute ( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo ('Error establishing Connection');
exit ();
}
}
// Return all records as an associative array.
public function getQuotesAsArray() {
$query = "SELECT * FROM quotations WHERE flagged = 0 ORDER BY rating DESC";
$stmt = $this->DB->prepare($query);
$stmt->execute ();
// fetchall returns all records in the set as an array
return $stmt->fetchAll ( PDO::FETCH_ASSOC );
}
// Checks if a user exists, if not register the new user
public function register($username, $password) {
$query = "SELECT * from users";
$stmt = $this->DB->prepare($query);
$stmt->execute ();
// fetchall returns all records in the set as an array
$array = $stmt->fetchAll ( PDO::FETCH_ASSOC );
// If there are no registered users, just register the new user
if(count($array) == 0) {
$query = "INSERT INTO users (username, hash) VALUES ('$username',
'$password')";
$stmt = $this->DB->prepare($query);
$stmt->execute ();
return true;
// header( 'Location: index.php' );
}
foreach($array as $item) {
if($username == $item['username']) {
return false;
}
}
$query = "INSERT INTO users (username, hash) VALUES ('$username', '$password')";
$stmt = $this->DB->prepare($query);
$stmt->execute ();
return true;
//header( 'Location: index.php' );
}
public function addQuote($quote, $author) {
$query = "INSERT INTO quotations (added, quote, author, rating, flagged)
VALUES(now(), '$quote', '$author', 0, 0)";
$stmt = $this->DB->prepare($query);
$stmt->execute ();
}
} // End class DatabaseAdaptor
$theDBA = new DatabaseAdaptor();
?>
Any help is appreciated. Thanks!
答案 0 :(得分:0)
header();
替换为echo 'redirecting...';
,看看您的代码是否达到标题部分或不是。header
运行。例如,header
之前可能会出现警告或其他内容。在这种情况下,尝试解决该警告或(不是一个好主意)尝试ob_start
和ob_end_clean
以防止刷新任何输出。答案 1 :(得分:0)
我认为代码中存在问题:
if(isset($_GET['p']) && isset($_GET['q'])) {
$user = isset($_GET['p']);
$pass = isset($_GET['q']);
}
$ user和$ pass将始终获得值0或1,因此代码的下一部分
for ($i = 0; $i < count($quotes); $i++){
if($user === $quotes[$i][0]) {
$_SESSION['user'] = 'Rick';
header('Location: index.php');
}
}
不太可能评估为true(除非$ quotes数组的值为0或1),并且标题重定向不会触发。
在我看来,用户和密码应设置如下:
if(isset($_GET['p']) && isset($_GET['q'])) {
$user =$_GET['p'];
$pass = $_GET['q'];
}
希望这是问题。