Use javascript within PHP


I need to enter a specific password, if it is correct to redirect me to another page.

Do I have to add javascript code?

$password = "123456";
if ($_POST['password'] != $password) {
<form name="form" method="post" action="">
<input type="password" name="password"><br>
<input type="submit" value="Login"></form>
asked by davescode 13.09.2018 в 10:21

1 answer


You can use header() to submit a HTTP header Location that redirects you to a new URL, so it is not necessary to use JavaScript, but you can add it after sending the headers to make sure in the following way:

$password = "123456";
/* Compruebo si el formulario ha sido enviado con isset y el contenido */
if (isset($_POST['password']) && $_POST['password'] == $password) {
    /* Redirigimos al usuario a la nueva página */
    header('Location: pagina_secreta.html');
    /* Finalizamos la ejecución para que no salga el formulario de nuevo */
<form name="form" method="post"
  action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
    <input type="password" name="password"><br />
    <input type="submit" value="Login">
answered by 13.09.2018 в 10:30