【发布时间】:2014-11-26 22:03:09
【问题描述】:
首先,对于措辞不佳的标题问题,我们深表歉意。我正在编写一个小程序,其中 login() 函数在调用时会从数据库中获取“用户名”和“用户级别”。如果输入有效,则将用户带到注销页面。它所做的只是欢迎用户并提示他们注销。但是,当我使用以下代码时,它不显示用户信息:
logoutForm.inc.html...
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome</title>
<link rel="icon" type="image/png" href="http://upload.wikimedia.org/wikipedia/commons/a/ac/Farm-Fresh_key.png">
<meta charset="utf-8">
</head>
<body>
<div id = "wrapper">
<div id="masthead">
<h2>Welcome, <?php echo $userName; ?>!</h2>
<h3>Your current user level is <?php echo $userLevel; ?></h3>
<form action method="post" name="logoutForm">
<p>
<input type="submit" name="logout" value="Log Out" />
</p>
</form>
</div>
</div>
</body>
</html>
functions.php...
<?php
function login($userName)
{
if (empty($userName)){
echo '<h2>You forgot to enter something</h2>
<meta http-equiv="refresh" content="2; url=login.php" />';
}
else
{
$conn = new PDO("mysql:host=localhost:3307;dbname=users", "root", "");
$result = $conn->prepare("SELECT * FROM user_list WHERE email = :username");
$result->bindParam(':username', $userName);
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
if($row > 0) {
if ($row['userLevel'] == 'A')
{
$userLevel = 'Admin';
}
elseif ($row['userLevel'] == 'M')
{
$userLevel = 'Member';
}
header("Location: includes/logoutForm.inc.html");
}
else
{
echo '<h2>That entry is INVALID</h2>
<meta http-equiv="refresh" content="2; url=login.php" />';
}
}
}
function logout($userName)
{
session_start();
echo '<title>Logout</title>
<link rel="icon" type="image/png" href="http://upload.wikimedia.org/wikipedia/commons/a/ac/Farm-Fresh_key.png">
<meta charset="utf-8">';
$_SESSION['username'] = array();
session_destroy();
$name = session_name(); $expire = strtotime('-1 year'); $params = session_get_cookie_params(); $path = $params['path']; $domain = $params['domain']; $secure = $params['secure']; $httponly = $params['httponly'];
setcookie($name, '', $expire, $path, $domain, $secure, $httponly);
die('<h3>You have successfully logged out</h3>
<meta http-equiv="refresh" content="2; url=login.php" />');
}
更不用说 logout() 函数完全搞砸了。一次解决一个问题。感谢您的关注,如果可以,请提供帮助。
【问题讨论】:
-
好吧,除非您更改了服务器设置,否则 html 文件通常不会作为 php.ini 文件处理。此外,它被命名为 logoutForm。 inc .html 表明它旨在包含在 php 文件中,而不是直接访问
-
如果您要重定向,您需要将变量传递给
header,否则他们看不到您的$userLevel和$userName。您将不得不使用$_GET或包含该文件而不是将其交给。 -
我同意史蒂夫的观点,并且还想指出-您的登录/注销功能正是-功能。您应该调用该函数来确定用户是否有效,并根据函数响应向用户提供输出。
-
@JamesShaver 谢谢。我正在尝试使用 php 脚本作为控制器。即使我在调用登录/注销函数的脚本中使用了一个开关,并根据结果显示注销/登录 html 页面,如何将从 pdo 检索到的数据传递给 html?跨度>
-
@Rasclatt 这听起来很酷!如何使用标头传递变量?
标签: php html function pdo echo