PHP die()清理所有页面

时间:2012-12-06 02:28:14

标签: php

一个php死亡功能问题。 当我使用die()时,它会清除所有页面元素。 有没有办法回显错误消息而不是清理所有页面,当我使用die()来停止代码并调出消息时,看起来像跳转到另一个页面。

这是我的代码

    <?PHP
$message="";
if(isset($_POST['submit'])){

    $name=$_POST['name'];
    $password=$_POST['password'];

    //Field check
    if($name && $password){$message=$name . $password;}
    else{die($message="please enter name and password");}

    //Check name    
    if($name=="alex" && $password==123){$message="Welcome ". $name;}    
    else{$message="wrong user or password";}
    }
?>

<html>
<p>SIGN UP</p>
    <form action="testing.php" method="POST">
            <input type="text" name="name" placeholder="Enter Name" />
            <input type="password" name="password" placeholder="Enter Password"/>
            <input type="submit" name="submit" value="Sign up"/>
    </form>
    <div><?PHP echo $message?></div>
</html>

1 个答案:

答案 0 :(得分:3)

您应该从上到下阅读您的脚本,包括<?php ?>以外的任何内容。使用die()时,您的脚本就会停止。

<?php $a = "something"; ?>
<html>
  <p><?php echo $a?></p>
  <?php die(); ?>
  <p>Never here</p>
</html>

输出

<html>
  <p>something</p>

在你的情况下做

<?php
if(isset($_POST['submit'])){

    $name=$_POST['name'];
    $password=$_POST['password'];

    //Field check
    if(!$name || !$password) {
       $message="please enter name and password");

    //Check name and password    
    } elseif ($name=="alex" && $password=="alex1") {
       $message="Welcome ". $name;

    } else {
       $message="Username or password incorrect"
    }
?>
<html>
<p>SIGN UP</p>
    <form action="testing.php" method="POST">
            <input type="text" name="name" placeholder="Enter Name" />
            <input type="password" name="password" placeholder="Enter Password"/>
            <input type="submit" name="submit" value="Sign up"/>
    </form>
    <div><?php echo $message?></div>
</html>

另请注意,我使用'=='进行比较,而不是'='。