Home > Blockchain >  Redirect page and edit something in another file
Redirect page and edit something in another file

Time:12-08

i have a little problem I have a form in login.php: `

<form action="loginscript.php" method="post">
    <h2>Login form</h2>
    <label for="nick">Podaj imie: </label>
        <input type="text" name="nick" id="nick">
    <br>
    <label for="pass">Podaj haslo: </label>
    <input type="password" name="pass" id="pass">
    <br>
    <p>LOG IN</p>
    <input type="submit" name="submit" id="submit">
</form>

`

and i have a loginscript.php file: `

<?php
    session_start();
        if (isset($_POST["nick"]) && isset($_POST["pass"])) {

        $nick=$_POST["nick"];
        $pass=sha1(sha1($_POST["pass"]));

        $conn = mysqli_connect("localhost", "root", "", "baza2");
        if ($conn) {
            $query = mysqli_query($conn, "SELECT * FROM login_table WHERE nick='$nick' AND pass='$pass'");

            if (mysqli_num_rows($query)) {
                $_SESSION["logged"]=true;
                header("Location: main.php");
            } else {
                header('Location: login.php');
            }
            mysqli_close($conn);
        }  
    }
?>

`

In the loginscript.php in else i have redirect to login.php page. How can i change maybe p tag from 'LOG IN' to 'USERNAME OR PASSWORD IS WRONG'?

I tried using jquery but that doesn't work, maybe I don't know how. Please help :(

CodePudding user response:

You can't change anything on the target page from there, but what you can do is provide some information to the target page which that page can use. For example, consider this redirect:

header('Location: login.php?failed=true');

Then in the login.php code you can check for the "failed" query string value and conditionally change the output based on that. For example:

<?php
  $message = isset($_GET['failed']) ? "USERNAME OR PASSWORD IS WRONG" : "LOG IN";
?>
<form action="loginscript.php" method="post">
    <h2>Login form</h2>
    <label for="nick">Podaj imie: </label>
        <input type="text" name="nick" id="nick">
    <br>
    <label for="pass">Podaj haslo: </label>
    <input type="password" name="pass" id="pass">
    <br>
    <p><?= $message ?></p>
    <input type="submit" name="submit" id="submit">
</form>

CodePudding user response:

you could try the code below.

if (mysqli_num_rows($query)) {
    $_SESSION["logged"]=true;
    echo "<script type='text/javascript'> document.location = 'main.php';</script>";
} else {
    echo "<script>alert('Your Password or username is wrong!');</script>";
}
  • Related