PHP no carga el resto de la página después de salir;

I'm very new to PHP, and I can't figure out why this is happening.

For some reason, when exit fires the entire page stops loading, not just the PHP script. Like, it'll load the top half of the page, but nothing below where the script is included.

Aquí está mi código:

$page = $_GET["p"] . ".htm";
  if (!$_GET["p"]) {
    echo("<h1>Please click on a page on the left to begin</h1>\n");
    // problem here
    exit;
  }
  if ($_POST["page"]) {
    $handle = fopen("../includes/$page", "w");
    fwrite($handle, $_POST["page"]);
    fclose($handle);
    echo("<p>Page successfully saved.</p>\n");
    // problem here
    exit;
  }  
  if (file_exists("../includes/$page")) {
    $FILE = fopen("../includes/$page", "rt");
    while (!feof($FILE)) {
        $text .= fgets($FILE);
    }
    fclose($FILE);
  } else {
    echo("<h1>Page &quot;$page&quot; does not exist.</h1>\n");
    // echo("<h1>New Page: $page</h1>\n");
    // $text = "<p></p>";
    // problem here
    exit;
  }

preguntado el 28 de agosto de 11 a las 01:08

exit stops all page processing, right there, right then. So does die. No line after that will run, regardless of where or when it is in the code. It's equal to Full Stop. -

2 Respuestas

Even if you have HTML code following your PHP code, from the web server's perspective it is strictly a PHP script. When exit() is called, that is the end of it. PHP will output process and output no more HTML, and the web server will not output anymore html. In other words, it is working exactly as it is supposed to work.

If you need to terminate the flow of PHP code execution without preventing any further HTML from being output, you will need to reorganize your code accordingly.

Here is one suggestion. If there is a problem, set a variable indicating so. In subsequent if() blocks, check to see if previous problems were encountered.

$problem_encountered = FALSE;

  if (!$_GET["p"]) {
    echo("<h1>Please click on a page on the left to begin</h1>\n");

    // problem here

    // Set a boolean variable indicating something went wrong
    $problem_encountered = TRUE;
  }

  // In subsequent blocks, check that you haven't had problems so far
  // Adding preg_match() here to validate that the input is only letters & numbers
  // to protect against directory traversal.
  // Never pass user input into file operations, even checking file_exists()
  // without also whitelisting the input.
  if (!$problem_encountered && $_GET["page"] && preg_match('/^[a-z0-9]+$/', $_GET["page"])) {
    $page = $_GET["p"] . ".htm";
    $handle = fopen("../includes/$page", "w");
    fwrite($handle, $_GET["page"]);
    fclose($handle);
    echo("<p>Page successfully saved.</p>\n");

    // problem here
    $problem_encountered = TRUE;
  }  
  if (!$problem_encountered && file_exists("../includes/$page")) {
    $FILE = fopen("../includes/$page", "rt");
    while (!feof($FILE)) {
        $text .= fgets($FILE);
    }
    fclose($FILE);
  } else {
    echo("<h1>Page &quot;$page&quot; does not exist.</h1>\n");
    // echo("<h1>New Page: $page</h1>\n");
    // $text = "<p></p>";
    // problem here
    $problem_encountered = TRUE;
  }

There are lots of ways to handle this, many of which are better than the example I provided. But this is a very easy way for you to adapt your existing code without needing to do too much reorganization or risk breaking much.

contestado el 15 de mayo de 13 a las 23:05

You mean the location of the code on the page that's being displayed? There's no other way to stop the PHP without stopping everything? - JacobTheDev

@Rev see the example I provided. There are lots of ways to jump out of your PHP code without calling exit() - Michael Berkowski

There are lots of ways, but the code has to suit the function. exit (y die) should be considered specialty functions, not a way to end output while continuing the page execution, since it's not going to happen. - Jared Farrish

In PHP 5.3+ you can use the goto ambiental to jump to a label just before the ?> En lugar de usar exit in the example given in the question.

It would'n work well with more structured code (jumping out of functions), tough.

Maybe this should be a comment, who knows.

Respondido el 18 de enero de 17 a las 14:01

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.