我有一个 user.php(class) 、 login.php 和 index.php。当我在 index.php 中提交表单时,表单操作会转到 login.php,并且该文件使用用户类来验证是否存在尝试登录的人。如果为真,再次重定向到 index.file。但是我想在他/她登录时将用户全名写入 index.php。但我不能从课堂上取全名。
注意:在代码的 require_once 部分包括必要的文件,例如 database.php、session.php、functions.php 等。因此,它们没有错误,我的函数也没有错误。我想要的是如何在用户登录时将 full_name 写入 index.php。
这是user.php:
require_once $_SERVER['DOCUMENT_ROOT'].'/includes/database.php';
class User{
protected static $table_name="users";
protected static $db_fields = array('id', 'email', 'password', 'first_name',
'last_name');
public $id;
public $email;
public $password;
public $first_name;
public $last_name;
public function full_name() {
if(isset($this->first_name) && isset($this->last_name)) {
return $this->first_name . " " . $this->last_name;
} else {
return "";
}
}
public static function verify($user, $email="", $password="") {
global $database;
$sql = "SELECT * FROM users ";
$sql .= "WHERE email = '{$email}' ";
$sql .= "AND password = '{$password}' ";
$sql .= "LIMIT 1";
$result_array = self::find_by_sql($sql);
if (!empty($result_array)) {
$user->first_name = $result_array['first_name'];
$user->last_name = $result_array['last_name'];
return array_shift($result_array);
} else {
return false;
}
}
$user = new User();
//User class continue but i inserted necessary parts. All functions in the class works.
这是 login.php :
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/initialize.php'; ?>
<?
if($session->is_logged_in()) {
redirect_to("index.php");
}
if (isset($_POST['submit'])) {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
$found_user = User::verify($user,$email, $password);
if ($found_user) {
$session->login($found_user);
redirect_to("index.php");
} else {
$message = "There is error about username/password";
echo $message;
}
} else {
$email = "";
$password = "";
}
?>
这是 index.php :
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/includes/initialize.php'; ?>
<?php
if($session->is_logged_in()) {
echo $user->full_name();
} else {
echo "No name";
}
?>