简体   繁体   English

PHP登录和注册表单错误处理

[英]PHP login and registration form error handling

I want to make a nice error message inside of PHP and HTML like we make in bootstrap code, instead of showing a message in echo(). 我想在PHP和HTML中做一个很好的错误消息,就像我们在bootstrap代码中做的那样,而不是在echo()中显示消息。 but I don't want to use any function to make it. 但我不想使用任何功能来制作它。

<?php 
include 'condb.php';


if (isset($_POST['sub'])) {

$name = $_POST['name'];
$lname =$_POST['lname'];
$email =$_POST['email']; 
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];
if (empty($name) || empty($lname) || empty($email) || empty($pass1) || 
empty($pass2)) {
    echo "<p class='alert alert-danger'> must fill all </p>";
}else{

if (empty($pass1) || empty($pass2)) {
echo "you must fill password";

 }
else{

if ($pass1 == $pass2) {
    $hashpass = md5($pass1);

    $query="insert into users values (null, '$name', '$lname', '$email', 
'$hashpass')";
    $result=mysqli_query($conn,$query);

    echo "success";

}else{
    echo "passwords must be same";
}

}
}
}

?>

Store the message in a session variable like $_SESSION["errormsg"]='you must fill password'; 将消息存储在session变量中,如$_SESSION["errormsg"]='you must fill password';

Now you can echo the content of session variable in HTML like 现在你可以在HTML中回显会话变量的内容了

<div id='alert_msg'><?php if(isset($_SESSION["errormsg"])){ echo $_SESSION["errormsg"]; }?></div> 

Don't forget to include session_start() at the beginning of your script and unset the session after printing the content otherwise, it will show you the exact same content over and over. 不要忘记在脚本开头包含session_start()并在打印内容后取消设置会话,否则它会反复显示完全相同的内容。

// remove all session variables
 session_unset(); 

 // destroy the session 
 session_destroy(); 

if you work in different pages one is pure php and the another is embedded with the HTML, you make a session in php page and save your values in session and use the session in html page. 如果你在不同的页面工作,一个是纯PHP而另一个是嵌入HTML,你在php页面中创建会话并在会话中保存你的值并在html页面中使用会话。

<?php 
include 'condb.php';
session_start();
if (isset($_POST['sub'])) {

$name = $_POST['name'];
$lname =$_POST['lname'];
$email =$_POST['email']; 
$pass1 =$_POST['pass1'];
$pass2 =$_POST['pass2'];
if (empty($name) || empty($lname) || empty($email) || empty($pass1) || 
empty($pass2)) {
    $_SESSION["errormsg"]='you must fill password';
}else{

if (empty($pass1) || empty($pass2)) {
 $_SESSION["errormsg"] = "you must fill password";
 }
else{

if ($pass1 == $pass2) {
    $hashpass = md5($pass1);

    $query="insert into users values (null, '$name', '$lname', '$email', 
'$hashpass')";
    $result=mysqli_query($conn,$query);

    echo "success";

}else{
    $_SESSION["errormsg"] = "passwords must be same";
}

}
}
}

?>

in your html code use the session above. 在您的HTML代码中使用上面的会话。

<div id='alert_msg'><?php if(isset($_SESSION["errormsg"])){ echo $_SESSION["errormsg"]; }?></div> 

create a Separate file like messages.php and include it in your code like in header.php or in the separate page you want. 创建一个像messages.php这样的单独文件,并将其包含在您的代码中,如header.php或您想要的单独页面中。

Now in that message.php file, you can define all the messages you want to, It is easier to change and update them later on. 现在在该message.php文件中,您可以定义所需的所有消息,以后更容易更改和更新它们。 and easier to use them anywhere 更容易在任何地方使用它们

Example

define("ADVANCE_ADDED","Advance detail has been added successfully.");
define("ADVANCE_UPDATED","Advance detail has been updated successfully.");
define("ADVANCE_REMOVED","Advance detail has been removed successfully.");
define("APPROVE_LV_MSG","Are you sure you want to approve this leave?");
define("LV_APPROVED","You have approved the leave successfully.");
define("LV_REJECTED","You have rejected the leave successfully.");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM