开发者

PHP same page echoing (using die funct.)

开发者 https://www.devze.com 2023-01-13 13:30 出处:网络
<?php//filename signup.php ob_start(); include(\'dat.php\'); if (!empty($_POST)){ if ($pass!=\"d41d8cd98f00b204e9800998ecf8427e\" OR $user!=\"\") {// 1
    <?php                    //filename signup.php
    ob_start();
 include('dat.php');
    if (!empty($_POST)){


        if ($pass!="d41d8cd98f00b204e9800998ecf8427e" OR $user!="") {       // 1
            $result=mysql_query($sql);
            if(mysql_error()) {
                die('Sorry, this username already exists');                 // 2
            }
        } else {
        echo "Please enter the Password";
        }
    }

    ob_end_flush();
    ?>

<html>
<body>
<f开发者_C百科orm name="form1" method="post" action="signup.php">                <!--3-->
Username<input name="myusername" type="text" id="myusername" />
Password<input name="mypassword" type="text" id="mypassword" />
e-mail<input name="email" type="text" id="email" />
<input type="submit" name="Submit" value="Login" />
</form>
</body>
</html>

This is a sign up page (All the mysql details are missing here, they are included in dat.php) . If there is ny error , it is displayed in the same page.

problem 1 : [ //1 ] what is the operator for OR , in this case I have used the condition for if the pass or user is blank,but the OR is not working neither || , used in javascript.

problem 2 : [ //2 ] if die is used , then the statement is printed in the new window , but I want it to appear in the same window...I also used echo, it worked but I feel like i am missing the main function of 'die' by using 'echo'.

problem 3 : [ <!--3--> ] how can I replace signup.php as action to something like SERVER_PAGE or whatever...


  1. OR is the same as || except that OR has a lower precedence than ||. This can cause logical errors when it’s used with other operators with a higher precedence than OR but a lower precedence than || like the assignment operators:

    $var = false OR true;  // ($var = false) OR true;
    var_dump($var);        // bool(false)
    $var = false || true;  // $var = (false || true);
    var_dump($var);        // bool(true)
    

    So I recommend you to rather use || than OR.

  2. die does print the passed string and quits the execution of the current script. Personally, I wouldn’t use use die but implement a more decent error handling like storing the error message in a variable and print it in the document like this:

    $errors = array();
    if (!empty($_POST)) {
        if ($pass!="d41d8cd98f00b204e9800998ecf8427e" || $user!="") {
            $result=mysql_query($sql);
            if (mysql_error()) {
                $errors[] = 'Sorry, this username already exists';
            }
        } else {
            $errors[] = "Please enter the Password";
        }
    }
    if (!empty($errors)) {
        echo 'There were some errors:';
        echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
    }
    
  3. If you use an empty URL for the action attribute, it refers to the very same URL:

    <form name="form1" method="post" action="">
    

Some further tips:

  • Use $_SERVER['REQUEST_METHOD'] === 'POST' to test the request method instead of testing !empty($_POST).
  • Avoid register globals. So use $_POST['pass'] instead of $pass if you want to refer to the parameter pass passed by POST.


Use the following form tag POST to the same script... that way if you rename the file it'll continue to work.

<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> 


problem 1: || (pipe pipe is OR)

problem 2: die stops dead right where it was called. Find a way to return false to the other script and die there.

problem 3: Not sure what you want here.


  1. It is a logical operator. The same as || but with lower precedence.
    What do you mean by but the OR is not working neither || ? Do you get an exception? What should the result be? The syntax seems to be correct.
    Maybe you are using a wrong logical expression. What should it do? Currently it evaluates to true, if $pass is not d41d8cd98f00b204e9800998ecf8427e or if the $user is not empty.

  2. Read about die() in the manual, it is the same as exit():

    Output a message and terminate the current script

    It is perfectly valid to just use echo here, because you want that the script continues executing.

  3. What do you mean? Something like:

    <?php  define('SERVER_PAGE', 'signup.php'); ?>
    // later
    <form name="form1" method="post" action="<?php echo SERVER_PAGE ?>">
    

    ?


first one and Second one: As told by Gumbo

Answer for the third one: 3.<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> @Mikey1980 : thanks!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号