-2

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

james
  • 1
  • 2
  • 1
    Your `jump` function isn't a function. Double-check your syntax. Now is also a good time to start using your browser's debugging tools to observe errors and debug JavaScript code. – David Jan 12 '23 at 17:02
  • than you for your answer can you tell me how can i fix it please – james Jan 12 '23 at 17:26
  • 1
    Looking more at your JavaScript code, it's *full of typos and syntax errors*. Honestly, you'll want to just throw away this JavaScript code and start again. Consult some introductory JavaScript tutorials to start learning the syntax and structure of the language. As you develop your code, implement and test/validate *one* feature at a time. Only when you've confirmed that feature works as expected would you then move on to the next. There is no easy fix for the code shown, it's entirely invalid in many, many ways. – David Jan 12 '23 at 17:38
  • I apologize for my lack of understanding in javascript – james Jan 12 '23 at 18:43

1 Answers1

-1

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
}
lotmasi
  • 1
  • 2
  • it does noy work – james Jan 12 '23 at 18:41
  • Warning: Cannot modify header information - headers already sent by (output started at /home/runner/MobileWigglyImplementation/welcome.php:4) in /home/runner/MobileWigglyImplementation/welcome.php on line 26 – james Jan 12 '23 at 18:41