0

我正在用preg_match检查用户名,但我做错了,只是我不知道我做错了什么,我尝试了很多次,但没有成功,所以如果你能帮助我,非常感谢你,这也是一个很好的给我上一课。这是我正在做的事情的来源:

<?php
  if(isset($_POST['submit'])) 
  {
?>
<html>
<head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>
<script language="javascript" src="config.php?name=<?php echo $_POST['name']; ?>&amp;gender=<?php echo $_POST['gender']; ?>&amp;color=<?php echo $_POST['color']; ?>" type="text/javascript"></script>
</head>
<body>
<!-- Applet Start -->
<br /><div style="text-align: center;">
<div id="lightIRC" style="height:500px; text-align:center;"><p><a href="http://www.adobe.com/go/getflashplayer" title="Get flash player"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>

<script type="text/javascript">swfobject.embedSWF("http://www.futurechat.nl/chat/lightIRC.swf", "lightIRC", "100%", "500", "10.0.0", "http://www.futurechat.nl/chat/expressInstall.swf", params);</script>
</div>
</body>
</html>
<?php
}
  else {
  print_form();
}
function print_form() {
// MAKE SURE USERNAME IS ALPHANUMERIC
$name = $_POST['name'];
if(preg_match('/[^A-Za-z0-9_-]/', $_POST['name']))
  echo "Invalid Username";
?>
<html>
<head>
<title>Login</title>
<style>
body {font-family: Verdana;}
</style>
</head>
<body>
<form action="chatlogin.php" method="post">
<table cellspacing="0" cellpadding="0" align="center" style="background: #cccccc;text-align: center;font-size: 13px;">
<tr valign="top">
<td>
Nickname: 
<input type="text" name="name">
<br /><br />
Gender: 
<input type="radio" name="gender" value="Male">Male <input type="radio" name="gender" value="Female">Female
<br /><br />
Applet Color: 
<select name="color">
  <option value="lightblue">Lightblue</option>
  <option value="blue">Blue</option>
  <option value="darkorange">Dark Orange</option>
  <option value="black">Black</option>
  <option value="yellow">Yellow</option>
  <option value="green">Green</option>
  <option value="pink">Pink</option>
  <option value="white">White</option>
</select> 
<br /><br />
<input type="submit" name="submit" value="Chat now!">
</td>
</tr></table>
</form>
</body>
</html>
<?php
}
?>
4

3 回答 3

0

更新:OP需要检测无效字符,然后,不需要匹配整个单词,他对这个模式是正确的:

/[^A-Za-z0-9_-]/

真正的问题可以通过删除这部分代码来解决:

$name = $_POST['name'];
if(preg_match('/[^A-Za-z0-9_-]/', $_POST['name']))
  echo "Invalid Username";

到页面的开头:

<?php
  if(isset($_POST['submit'])) 
  {
    $name = $_POST['name'];
    if(preg_match('/[^A-Za-z0-9_-]/', $_POST['name']))
      $error = "Invalid Username";
?>

然后你可以$error在页面的任何地方回显

我也会在下面留下答案以获取一般信息。


您需要+在字符组末尾添加符号以匹配多个字符:

preg_match('/^[^A-Za-z0-9_-]+$/', $_POST['name'])

您的模式将在任何地方仅匹配一个字符,因此,您还需要^在开头和$结尾添加以匹配整个单词

于 2013-03-30T20:20:30.127 回答
0

设置好的时候看源码$_POST['name']吗?看起来您正在Invalid Username文档的最顶部、<html>标签上方打印,这就是它不会显示在浏览器中的原因(因为它不是有效的 HTML)。您应该存储错误消息并将其打印在 HTML 中,或者在 HTML 中移动逻辑(由<?php?>标签包围)

试试这个代码:

<?php
$ValidUsername = true;
if(isset($_POST['submit'])) {
    // MAKE SURE USERNAME IS ALPHANUMERIC
    $name = $_POST['name'];
    if(preg_match('/[^A-Za-z0-9_-]/', $name))
      $ValidUsername = false;
}
?>
<?php if(isset($_POST['submit']) && $ValidUsername): ?>
<html>
<head>
<script language="javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script>
<script language="javascript" src="config.php?name=<?php echo $_POST['name']; ?>&amp;gender=<?php echo $_POST['gender']; ?>&amp;color=<?php echo $_POST['color']; ?>" type="text/javascript"></script>
</head>
<body>
<!-- Applet Start -->
<br /><div style="text-align: center;">
<div id="lightIRC" style="height:500px; text-align:center;"><p><a href="http://www.adobe.com/go/getflashplayer" title="Get flash player"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></p></div>

<script type="text/javascript">swfobject.embedSWF("http://www.futurechat.nl/chat/lightIRC.swf", "lightIRC", "100%", "500", "10.0.0", "http://www.futurechat.nl/chat/expressInstall.swf", params);</script>
</div>
</body>
</html>

<?php else: ?>

<html>
<head>
<title>Login</title>
<style>
body {font-family: Verdana;}
</style>
</head>
<body>
<?php
if (!$ValidUsername)
    echo "<h1>Invalid Username</h1>";
?>
<form action="chatlogin.php" method="post">
<table cellspacing="0" cellpadding="0" align="center" style="background: #cccccc;text-align: center;font-size: 13px;">
<tr valign="top">
<td>
Nickname: 
<input type="text" name="name">
<br /><br />
Gender: 
<input type="radio" name="gender" value="Male">Male <input type="radio" name="gender" value="Female">Female
<br /><br />
Applet Color: 
<select name="color">
  <option value="lightblue">Lightblue</option>
  <option value="blue">Blue</option>
  <option value="darkorange">Dark Orange</option>
  <option value="black">Black</option>
  <option value="yellow">Yellow</option>
  <option value="green">Green</option>
  <option value="pink">Pink</option>
  <option value="white">White</option>
</select> 
<br /><br />
<input type="submit" name="submit" value="Chat now!">
</td>
</tr></table>
</form>
</body>
</html>
<?php endif; ?>

此代码使用替代语法来封装 HTML 代码。我相信它更容易阅读......

于 2013-03-30T20:22:36.817 回答
0

In your regular expression, the dash needs to be the first character in order to be used literally. See Also Allow - (dash) in regular expression

于 2013-03-30T20:52:36.907 回答