How to navigate to other page in html?

81 Views Asked by At

I'm a nood in phpand html also javascript, and I'm tring to make a login system with html and php and javascript if necesery but here is a problem which is i don't know how to navigate to other page after login (I tryed to use header() before but failed because it must beedn but at the froung of the code not after.

this is the start login code

<?php
session_start();
?>
<!DOCTYPE HTML>
<html>  
<body>

<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
Password: <input type="text" name="password"><br>
<input type="submit">
</form>

</body>
</html>

it's called index.php

and this is the login process page which called welcome.php

<?php
session_start();
?>
<html>
<body>
<p id="demo">hello</p>
<?php

$namee=fopen("name.txt", "r") or die("Unable to open file!");
$passworde=fopen("password.txt", "r") or die("Unable to open file!");

$name=fgets($namee);
$password=fgets($passworde);

$savename = fopen("savename.txt", "w");
$savepassword = fopen("savepassword.txt", "w");

$nameinput = $_POST["name"];
$passwordinput = $_POST["password"];

if ($name == htmlspecialchars($_POST["name"]) AND $password == htmlspecialchars($_POST["password"])) {
  echo $_POST["name"];
  echo $_POST["password"]; 
  fwrite($savename, $_POST["name"]);
  fwrite($savepassword, $_POST["password"]);
} else {
  echo "permission denied";
}
?>
<p id="dem">awef</p>
<script>
  window.alert(5 + 6);
  document.getElementById("demo").innerHTML = "wtf";
  var nameinput=<?php $_POST["name"]?>;
  var passwordinput=<?php $_POST["password"]; ?>;
  var name=<?php $name; ?>;
  var password=<?php $password; ?>;
  var closesavename=<?php fclose($savename); ?>;
  var closesavepassword=<?php fclose($savepassword); ?>;
  function jump{
    location.replace("https://MobileWigglyImplementation.gepingyi.repl.co/mainpage.php")
}
  if (nameinput == name AND passwordinput == password) {
    closesavename
    closesavepassword
    jump()
    exit()
    
</script>
</body>
</html>

and this is the page i want navigate to which is caled mainpage.php

<?php
// We need to use sessions, so you should always start sessions using the below code.
session_start();
// If the user is not logged in redirect to the login page...
$openname = fopen("savename.txt", "r");
$openpassword = fopen("savepassword.txt", "r");
$readname = fgets($openname);
$readpassword = fgets($openpassword);
$namee=fopen("name.txt", "r") or die("Unable to open file!");
$passworde=fopen("password.txt", "r") or die("Unable to open file!");
$name=fgets($namee);
$password=fgets($passworde);

if ($readname == $name AND $readpassword == $password) {
  echo "good";
  fclose($openname);
  fclose($openpassword);
  fclose($namee);
  fclose($passworde);
} else {
  fclose($openname);
  fclose($openpassword);
  fclose($namee);
  fclose($passworde);
  echo "erro";
}  
echo "good bro";
?>

<!DOCTYPE html>
<html>
<body>

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>
<h4>This is heading 4</h4>
<h5>This is heading 5</h5>
<h6>This is heading 6</h6>

</body>
</html>

this naem.txt

james

this is password.txt

123456

the output is this when I input james for name and 123456 for password

hello

james123456
awef

i was expecting it jump to mainpage.php

1

There are 1 best solutions below

2
On

you can use header for redirection. See below:

    if ($name == htmlspecialchars($_POST["name"]) AND $password == htmlspecialchars($_POST["password"])) {
    echo $_POST["name"];
    echo $_POST["password"];
    fwrite($savename, $_POST["name"]);
    fwrite($savepassword, $_POST["password"]);
    header("refresh:1; mainpage.php"); //Additional line here
}