您好我这个非常简单的注册页面我有3个文件:welcome.php,registration_form.php和register.php
这是我的welcome.php代码
<html>
<body>
<h1>WELCOME!</h1>
<form action="login.php" method="POST">
<p>Username: <input type="text" name="login_username" value=""> </p>
<p>Password: <input type="password" name="login_password" value=""></p>
<p><input type="submit" value="LOGIN" name="login" size="20"></p>
<a href="registration_form.php">Register for new account</a>
</body>
<html>
这是我在registration_form.php中的代码
<html>
<body>
<h1>Register here</h1>
<form action="register.php" method="POST">
<p>Username: <input type="text" name="register_username" value=""></p>
<p>Password: <input type="password" name="register_password" value=""></p>
<p>Re-type Password: <input type="password" name="register_repassword" value=""></p>
<p>E-mail Address: <input type="text" name="register_email" value=""></p>
<p>Re-type E-mail Address: <input type="text" name="register_reemail" value=""></p>
<p><input type="submit" value="Register" name="register"></p>
</form>
</body>
<html>
这是我的register.php
<html>
<body>
<?php
ob_start();
//=======================database variables
$host="localhost";
$db_username="root";
$db_password="";
$db="forum_members";
$db_table="members";
//=======================connect to database
mysql_connect("$host","$db_username","$db_password") or die("Could not connect to the database!");
mysql_select_db("$db") or die("database not found!");
//form variables
$register_user=$_POST['register_username'];
$register_pass=$_POST['register_password'];
$register_repass = $_POST['register_repassword'];
$register_email=$_POST['register_email'];
$register_reemail=$_POST['register_reemail'];
//protect database from MySQL database
$register_user=stripslashes($register_user);
$register_pass=stripslashes($register_pass);
$register_repass=stripslashes($register_repass);
$register_email=stripslashes($register_email);
$register_reemail=stripslashes($register_reemail);
$register_user=mysql_real_escape_string($register_user);
$register_pass=mysql_real_escape_string($register_pass);
$register_repass=mysql_real_escape_string($register_repass);
$register_email=mysql_real_escape_string($register_email);
$register_reemail=mysql_real_escape_string($register_reemail);
//check required fields
if (empty($register_user) || empty($register_pass)) {
echo "Please fill the required fields";
die();
}
if (empty($register_repass) || empty($register_email)) {
echo "Please fill the required fields";
die();
}
if (empty($register_reemail)) {
echo "Please fill the required fields";
die();
}
//check if username has alphanumeric characters only
if (!preg_match("/^[a-zA-Z0-9_]+$/", $register_user) || !preg_match("/^[a-zA-Z0-9_]+$/", $register_pass)) {
echo "Username and Password can only contain alphanumeric characters";
die();
}
//check username and password minimum length
if (strlen($register_user) < 4) {
echo "Username must be more than 4 characters!";
die();
}
if (strlen($register_pass) < 8) {
echo "Password must be at least 8 characters!";
die();
}
if ($register_pass !== $register_repass) {
echo "Your password did not match";
die();
}
if ($register_email !== $register_reemail) {
echo "Your E-mail address did not match";
die();
}
//check duplicate username
$duplicate_user="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_duplicate=mysql_query($duplicate_user);
$duplicate_result = mysql_num_rows($execute_duplicate);
if ($duplicate_result == 1) {
echo "This username is already used";
die();
}
//create MySQL Query
$query_insert="INSERT INTO $db_table(username, password, email) VALUES ('$register_user', '$register_pass', '$register_email')";
//execute MySQL query
$execute_insert=mysql_query($query_insert);
//$execute_insert=mysql_query($query_insert);
//check inserted data
$check_insert="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_insert1=mysql_query($check_insert);
$verify_insert=mysql_num_rows($execute_insert1);
if ($verify_insert==1) {
echo "Registration Successful! You may now login";
}
else {
echo "Registration Failed!";
}
ob_end_flush();
?>
</body>
</html>
我的问题是如何使用$ _SERVER ['PHP_SELF']以便我可以合并registration_form.php和register.php?这样我就不会处理多个文件了。我的目标是在同一页面(最好是在顶部)显示“注册成功!您现在可以登录”或“注册失败”,当客人没有输入任何信息并点击“注册”按钮时,它将会显示回到登记表,好像什么也没发生。我试图在谷歌寻找答案,但它不起作用。
P.S。我知道我的代码中存在很多缺陷,请保持良好状态。我刚开始研究php。
答案 0 :(得分:2)
$_SERVER['PHP_SELF']
用于在同一页面上调用表单操作。所以你应该以表格形式给予它。
现在你在register.php上做了什么,如果数据已发布,只需在条件相同的页面上添加.. if(isset)
将此代码放入registration_form.php
<?php
ob_start();
//=======================database variables
$host="localhost";
$db_username="root";
$db_password="";
$db="forum_members";
$db_table="members";
//=======================connect to database
mysql_connect("$host","$db_username","$db_password") or die("Could not connect to the database!");
mysql_select_db("$db") or die("database not found!");
?>
<h1>Register here</h1>
<?php
// To confirm form is submitted
if(isset($_POST['register']))
{
//form variables
$register_user=$_POST['register_username'];
$register_pass=$_POST['register_password'];
$register_repass = $_POST['register_repassword'];
$register_email=$_POST['register_email'];
$register_reemail=$_POST['register_reemail'];
//protect database from MySQL database
$register_user=stripslashes($register_user);
$register_pass=stripslashes($register_pass);
$register_repass=stripslashes($register_repass);
$register_email=stripslashes($register_email);
$register_reemail=stripslashes($register_reemail);
$register_user=mysql_real_escape_string($register_user);
$register_pass=mysql_real_escape_string($register_pass);
$register_repass=mysql_real_escape_string($register_repass);
$register_email=mysql_real_escape_string($register_email);
$register_reemail=mysql_real_escape_string($register_reemail);
//check required fields
if (empty($register_user) || empty($register_pass)) {
echo "Please fill the required fields";
die();
}
if (empty($register_repass) || empty($register_email)) {
echo "Please fill the required fields";
die();
}
if (empty($register_reemail)) {
echo "Please fill the required fields";
die();
}
//check if username has alphanumeric characters only
if (!preg_match("/^[a-zA-Z0-9_]+$/", $register_user) || !preg_match("/^[a-zA-Z0-9_]+$/", $register_pass)) {
echo "Username and Password can only contain alphanumeric characters";
die();
}
//check username and password minimum length
if (strlen($register_user) < 4) {
echo "Username must be more than 4 characters!";
die();
}
if (strlen($register_pass) < 8) {
echo "Password must be at least 8 characters!";
die();
}
if ($register_pass !== $register_repass) {
echo "Your password did not match";
die();
}
if ($register_email !== $register_reemail) {
echo "Your E-mail address did not match";
die();
}
//check duplicate username
$duplicate_user="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_duplicate=mysql_query($duplicate_user);
$duplicate_result = mysql_num_rows($execute_duplicate);
if ($duplicate_result == 1) {
echo "This username is already used";
die();
}
//create MySQL Query
$query_insert="INSERT INTO $db_table(username, password, email) VALUES ('$register_user', '$register_pass', '$register_email')";
//execute MySQL query
$execute_insert=mysql_query($query_insert);
//$execute_insert=mysql_query($query_insert);
//check inserted data
$check_insert="SELECT * FROM $db_table WHERE username='$register_user'";
$execute_insert1=mysql_query($check_insert);
$verify_insert=mysql_num_rows($execute_insert1);
if ($verify_insert==1) {
echo "Registration Successful! You may now login";
}
else {
echo "Registration Failed!";
}
ob_end_flush();
}
?>
<?php // To call form on same page ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>Username: <input type="text" name="register_username" value=""></p>
<p>Password: <input type="password" name="register_password" value=""></p>
<p>Re-type Password: <input type="password" name="register_repassword" value=""></p>
<p>E-mail Address: <input type="text" name="register_email" value=""></p>
<p>Re-type E-mail Address: <input type="text" name="register_reemail" value=""></p>
<p><input type="submit" value="Register" name="register"></p>
</form>
</body>
<html>
答案 1 :(得分:0)
这是什么?
$_SERVER['PHP_SELF']
不用于在同一页面上调用表单操作,但您可以这样使用它。
$_SERVER['PHP_SELF']
包含当前正在执行的脚本的文件名,相对于文档根目录。这意味着如果你在http://domain.com/path/to/file.php
那么
$_SERVER['PHP_SELF']
将是/path/to/file.php
。
如何使用?
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
如何合并?
$_SERVER['REQUEST_METHOD']
包含当前的请求方法。即GET,POST,PUT
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// handle your form submition here
}
?>
<html>
...
<!-- show register form -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
如何避免因刷新页面而提交?
将用户重定向到其他地方。
header("Location: /path/to/somewhere");
但您不喜欢文件,因此请将用户重定向到当前网址。
header("Location: {$_SERVER['PHP_SELF']}");
一体化:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
// handle your form submition here
// everything is ok.
header("Location: {$_SERVER['PHP_SELF']}");
}
?>
<html>
...
<!-- show register form -->
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">