0
我一直在玩登錄系統,我已經有一個非常好的(基本的,但很好)的內置。我似乎無法開始工作的一件事就是讓註冊用戶在提交註冊表單後自動登錄(顯然,用戶正在插入)。PHP新用戶自動登錄
這裏是我的嘗試:
*編輯:加滿級,login.tpl.php和myProfile.php。我爲所有的代碼片斷道歉!
登錄類:
<?php
class login
{
protected $_email;
protected $_password;
protected $hash;
protected $_db;
protected $_user;
public function __construct(PDO $db)
{
$this->_db = $db;
}
public function validate()
{
$query = $this->_db->prepare('SELECT * FROM users WHERE email=?');
$query->execute(array($this->_email));
if ($query->rowcount() > 0)
{
$user = $query->fetch(PDO::FETCH_ASSOC);
if (password_verify ($this->_password , $user['password']))
{
return $user;
}
}
return false;
}
public function login($email, $password)
{
$this->_email = $email;
$this-> _password = $password;
$user = $this->validate();
if ($user)
{
$_SESSION['user_id'] = $user['id'];
return $user['id'];
}
return false;
}
public function createUser($first_name, $last_name, $email, $password)
{
$this->hash = password_hash($password, PASSWORD_BCRYPT);
$query = $this->_db->prepare("INSERT INTO users (email, password, first_name, last_name) VALUES (:email, :password, :first_name, :last_name)");
$query->execute(array(
":email"=> $email,
":password"=> $password,
":first_name"=> $first_name,
":last_name"=> $last_name));
}
public function logout()
{
session_destroy();
}
public function getUserData()
{
$this->_user = $_SESSION['user_id'] ;
$query = $this->_db->prepare('SELECT * FROM users WHERE id=?');
$query->execute(array($this->_user));
return $query->fetch(PDO::FETCH_ASSOC);
}
public function uploadPicture($uploaded)
{
$targetPath = $_SERVER['DOCUMENT_ROOT']; $targetPath .= "/wdv441/userLogin/app/views/img/";
$pathinfo = pathinfo($uploaded['name']);
$filesize = $uploaded['size'];
$fileName = "profilePic". $this->_user . ".png";
$ok = 1;
$KB = 1024;
$MB = 1048576;
if ($filesize > 400*$KB)
{
echo "File too big.";
$ok = 0;
}
else
{
if (move_uploaded_file($uploaded['tmp_name'], $targetPath . $fileName))
{
echo "File " . $fileName . " has been uploaded.";
}
else
{
echo "File not uploaded";
}
}
}
public function getPicture()
{
$targetPath = $_SERVER['DOCUMENT_ROOT']; $targetPath .= "/wdv441/userLogin/app/views/img/";
$fileName = "profilePic". $this->_user . ".png";
$image = null;
if (file_exists($targetPath . $fileName))
{
$image = $fileName;
}
else
{
$image = "default.png";
}
return $image;
}
}
?>
register.php:
<?php
require_once($loginClassPath);
session_start();
if (empty($_SESSION['user_id']))
{
try {
$pdo = new PDO($dsn, $db_username, $db_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e){
echo "Error connecting to database. Error" . $e->getmessage;
}
if ($pdo)
{
$loginClass = new login($pdo);
if (isset($_POST['submit']))
{
$allFields = $_POST['first_name'] . $_POST['last_name'] . $_POST['email'] . $_POST['password'];
if(!empty($allFields))
{
if($loginClass->createUser($_POST['first_name'] , $_POST['last_name'] , $_POST['email'] , $_POST['password']))
{
if ($user_id = $loginClass->login($_POST['email'], $_POST['password']))
{
header('Location: myProfile.tpl.php');
die();
}
}
}
else
{
$errMsg = "red";
}
}
}
}
else
{
header('Location: myProfile.tpl.php');
die();
}
?>
register.tpl.php:
<?php
$errMsg="";
require_once($registerPath);
?>
<html>
<head>
<title>User login</title>
</head>
<body>
<div style="text-align:center; margin-left:auto; margin-right:auto;">
<h3>Please Fill out all fields below: </h3>
<form method="post" action="<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>">
<span style="color:<?php echo $errMsg; ?>;">All fields are required</span>
<p>First Name: </p>
<input type ="text" name="first_name" />
<p>Last Name: </p>
<input type ="text" name="last_name" />
<p>Email: </p>
<input type ="text" name="email" />
<p>Password: </p>
<input type="password" name ="password"/>
<p><input type="submit" name ="submit" value="Register"/></p>
</form>
</div>
</body>
</html>
login.tpl.php
<?php
$errMsg=" ";
require($loginPath);
?>
<html>
<head>
<title>User login</title>
</head>
<body>
<div style="text-align:center; margin-left:auto; margin-right:auto;">
<h3>Please login below: </h3>
<form method="post" action=<?php echo htmlspecialchars($_SERVER['SCRIPT_NAME']); ?>>
<span style="color:red;"><?php echo $errMsg ?></span>
<p>Username: </p>
<input type ="text" name="email" />
<p>Password: </p>
<input type="password" name ="password"/>
<p><input type="submit" name ="login" value="Login"/></p>
<p>Don't have an account? <a href="register.tpl.php">Register here</a>!</p>
<form>
</div>
</body>
</html>
當前,當新用戶註冊時,會將用戶踢到登錄屏幕。這是因爲當它重定向到「myProfile.php」我已經在爲了讓人們簽署下面的「myProfile.php」代碼:
myProfile.php:
<?php
require_once($loginClassPath);
session_start();
if (!empty($_SESSION['user_id']))
{
try
{
$pdo = new PDO($dsn, $db_username, $db_password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e)
{
echo "Error connecting to database. Error" . $e->getmessage;
}
if ($pdo)
{
$loginClass = new login($pdo);
$userData = $loginClass->getUserData();
if (isset($_GET['logout']))
{
if ($_GET['logout'] == 'yes')
{
$loginClass->logout();
header('Location: login.tpl.php');
die();
}
}
}
}
else
{
header('Location: login.tpl.php');
die();
}
?>
我的問題是基本上我哪裏錯了?我在這裏關閉還是離開基地?
如果已經有類似的問題,我提前致歉,我環顧了一會兒,但找不到任何幫助我的東西。如果我沒有提供足夠的信息,請告訴我!
在此先感謝你們!
login.tpl.php文件的內容是什麼? – 2015-03-25 05:01:45
從我看來,它看起來並不像你在代碼中專門定義了一個實際的類,所以'new login()'會拋出一個錯誤,除非你沒有顯示它? – 2015-03-25 05:20:15
我確實有整個班級。爲了縮短問題,我只是抽象了代碼片段中正在使用的函數。如果你想讓我添加整個班級,我可以! – RoyaleWCheese1 2015-03-25 05:21:56