我有一个user.php(类),login.php和index.php。当我在index.php中提交表单时,表单操作转到login.php,此文件使用User Class来验证是否存在尝试登录的人。如果为true,则再次重定向到index.file。但我想在用户登录时将用户full_name写入index.php。但是我不能从课堂上获取full_name。
注意:在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";
}
?>
答案 0 :(得分:0)
将full_name的输出保存到会话变量。然后可以调用它来重现名称。
有关在PHP中使用会话的信息,请使用手册:http://www.php.net/manual/en/ref.session.php