所以我有一个管理面板,我只是为了体验而创建的测试目的,我正在努力使它,当你登录(使用PHP会话),你可以点击按钮配置文件,它会带你到你的配置文件,并显示您的姓名,电子邮件,用户组。我的数据库中有所有的它们,但是我似乎不能显示它们。
以下是我的代码
<?php
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';
sec_session_start();
if (isset($_GET['username'])) {
$username = mysql_real_escape_string($_GET['username']);
if (ctype_alnum($username)) {
//check user exists
$check = mysql_query("SELECT username, email, usergroup FROM members WHERE username='$username'");
if (mysql_num_rows($check)===1) {
$get = mysql_fetch_assoc($check);
$username = $get['username'];
$email = $get['email'];
$usergroup = $get['usergroup'];
} else {
echo "<h2>User does not exist!</h2>";
exit();
}
}
}
?>
<html>
<head>
<title>test</title>
<body>
<php? echo $username; ?>
</body>
</html>那么你有什么想法吗?
谢谢,
还有-我在那里定义了$username作为一个变量,但当我显示它并加载页面时,它只显示未定义的变量: username in
发布于 2014-05-11 00:56:36
很难确定真正的错误发生在哪里。那么让我们来看看吧。
让我们从你的包含开始:
include_once 'includes/db_connect.php';
include_once 'includes/functions.php';这是两个未知的来源。所以很难说是不是出了什么问题。
如果您检查通过URI传递的用户名:
if (isset($_GET['username'])) {这可能会导致错误(虽然在你的例子中它不太可能调用错误),因为如果你传递一个没有值的GET变量键(例如,index.php?foo导致isset($_GET['foo']) == true,而!empty($_GET['foo']) == false。所以在这里用!empty()代替isset()是很方便的。
检查用户名是否为字母数字:
if (ctype_alnum($username)) {由于ctype_alnum() "checks if all of the characters in the provided string [...] are alphanumeric",该错误可能是由于传递包含非字母数字字符用户名造成的。
您没有用于字母数字检查的else {}定义。考虑这样做,例如通过添加
if(ctype_alumn(...)) {
// ...
} else {
echo "<h2>Alphanumeric username required!</h2>";
exit();
}你的MySQL抓取看起来没问题,否则你的
echo "<h2>User does not exist!</h2>";
exit();就会把它拿过来。
最后,还要考虑为您的isset($_GET['username'])检查添加else用例,例如
if (isset($_GET['username'])) {
// ...
} else {
echo "<h2>No username provided!</h2>";
exit();
}关于var_dump()用法的旁注
@Andy评论了使用var_dump()转储有关$_GET['username']的信息的建议。下面是一个示例:
if (isset($_GET['username'])) {
var_dump($_GET['username']);
exit();
}将打印出类似这样的内容
string(7) "Foo Bar"https://stackoverflow.com/questions/23554724
复制相似问题