Use javascript within PHP

2

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

Do I have to add javascript code?

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

1 answer

1

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:

<?php
$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 */
    die('<script>window.location.assign("pagina_secreta.html")</script>');
}
?><h2>Logueate</h2>
<form name="form" method="post"
  action="<?= htmlspecialchars($_SERVER['PHP_SELF']) ?>">
    <input type="password" name="password"><br />
    <input type="submit" value="Login">
</form>
    
answered by 13.09.2018 в 10:30