我认为我正在使用php oop,但是它没有将页面重定向到profile.php页面,我提交后它就会变成空白。
没有错误信息显示,我想知道我的错误是什么。
可能是session problem吗?
的index.php
<?php
error_reporting(-1);
require_once 'layouts/header.php';
require_once 'User.php';
session_start();
if (isset($_POST['btn-signup']) ){
$username = htmlentities($_POST['txt-username']);
$unpass = htmlentities($_POST['txt-password']);
$password = password_hash($unpass, PASSWORD_BCRYPT, ['cost' => 12] );
$unemail = $_POST['txt-email'];
$email = filter_var($unemail, FILTER_VALIDATE_EMAIL);
$guest = new User();
if($username ==""){
echo "Enter a Username";
}
if($email == ""){
echo "Enter a Email";
}
if($password == ""){
echo "Enter a Password";
}
elseif($guest->signup($email,$password,$username)){
header("Location:profile.php");
}
}
?>
<div class="container">
<div class="row">
<form action ="" method="POST">
<div class="form-group">
<label for="Email">Email address</label>
<input type="email" class="form-control" aria-describedby="emailHelp" name="txt-email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="Username">Username</label>
<input type="text" class="form-control" name="txt-username" aria-describedby="emailHelp" name="txt-username" placeholder="Enter Username">
</div>
<div class="form-group">
<label for="Password">Password</label>
<input type="password" class="form-control" name="txt-password" aria-describedby="emailHelp" name="txt-password" placeholder="Enter email">
</div>
<button type="submit" name="btn-signup" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</body>
</html>
user.php的
<?php
require_once 'Db.php';
class User extends Db{
private $db;
public function __construct()
{
$this->db = new Db();
}
public function signup($email, $password, $username)
{
try{
$stmt = $this->db->connect->prepare("INSERT INTO users (user_email, user_pass, user_name) VALUES (:email, :password, :username) ");
$stmt->bindparam(':email', $email);
$stmt->bindparam(':password', $password);
$stmt->bindparam(':username', $username);
$stmt->execute();
}
catch(PDOExeception $e)
{
echo $e->getMessage();
}
}
}
db.php中
<?php
error_reporting(-1);
class Db{
private $db_host;
private $db_user;
private $db_name;
private $db_pass;
public function connect()
{
$this->db_host = "127.0.0.1";
$this->db_user = "root";
$this->db_pass = "root";
$this->db_name = "eli9";
try {
$db = new PDO("mysql:host=127.0.0.1;port=8889,dbname=eli9", 'root', 'root');
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
echo "connected \n";
}
catch (PDOException $e){
echo $e->getMessage();
}
return $db;
}
}
答案 0 :(得分:0)
session_start()
放在脚本的最开头。但在你的情况下,似乎
require_once 'layouts/header.php';
可能有一些HTML或其他输出。将session_start()
代码置于其上可以解决您的问题。
<?php
error_reporting(-1);
session_start();
require_once 'layouts/header.php';
require_once 'User.php';
if (isset($_POST['btn-signup']) ){
$username = htmlentities($_POST['txt-username']);
$unpass = htmlentities($_POST['txt-password']);
$password = password_hash($unpass, PASSWORD_BCRYPT, ['cost' => 12] );
$unemail = $_POST['txt-email'];
$email = filter_var($unemail, FILTER_VALIDATE_EMAIL);
$guest = new User();
if($username ==""){
echo "Enter a Username";
}
if($email == ""){
echo "Enter a Email";
}
if($password == ""){
echo "Enter a Password";
}
elseif($guest->signup($email,$password,$username)){
header("Location:profile.php");
}
}
?>