Hi Guys 🙂

I have a bit of a problem about a login system for a website. In the login page it should redirect to a page called admin.php but it sure don't.. Below I'll post both my admin.php and login.php files. Hope someone knows what my problem with my header() is.

Login.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Underside</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>
<table>
<p style="font-size: 25px;">Log Ind</p>
<tr>
<td>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
Username:
</td><td>
<input type="text" size="25" name="usr">
</td></tr><tr>
<td>
Password:
</td><td>
<input type="password" size="25" name="pwd">
</td></tr>
</table>
<input type="submit" name="Log_Ind" value="Log Ind">
</form>
</body>
</html>

<?php
$usr = $REQUEST["usr"];
$pwd = $
REQUEST["pwd"];

if($usr=="user" and $pwd=="password") {
session_start();
session_register('user');
header("location:frontpage.php?site=admin");
exit;
}
?>

admin.php

<?php
session_start();
if(session_is_registered('user'))
{
echo "You are admin";
}
else
{
header("Location:ip.php");
exit;
}
?>

Happy Debugging 🙂

Best Regards

Christian S.

    You cant change headers after something had been outputted onto the page.

    it looks like you have it on your login page after all the HTMl, it needs to be before any HTML is outputted.

      And use of session_register and session_is_registered are out of fashion(deprecated). Instead use:
      change:
      session_register('user');
      ..to
      $_SESSION['user'] = $usr;

      And in admin page change:
      if(session_is_registered('user'))
      ..to:
      if(isset($_SESSION['user']))

      You are lacking the user and password check but I quess you are just experimenting and learning..

        hello
        you can use window.location in javascript instead of header Function..
        if you want uses more than 1 times you can creat function like:

        <?
          function goto($var)
          {
          print "<script>window.location=\"$var\"</script>";
          }
        ?>
        

        😃

        <?
        goto("www.google.com");
        ?>

          Very simple...

          in login.php... this (after being correct, I mean session_register() replaced by $SESSION, which I DID NOT DO)...

          <?php
          $usr = $_REQUEST["usr"];
          $pwd = $_REQUEST["pwd"];
          
          if($usr=="user" and $pwd=="password") {
          session_start();
          session_register('user');
          header("location:frontpage.php?site=admin");
          exit;
          }
          ?>
          

          ... has to go to the very top of the page !

            suntra wrote:

            Very simple...

            in login.php... this (after being correct, I mean session_register() replaced by $_SESSION, which I DID NOT DO)...

            ...
            

            ... has to go to the very top of the page !

            Making sure to take the good advice from Cahva, is should be...

            <?php
            $usr = $_REQUEST["usr"];
            $pwd = $_REQUEST["pwd"];
            
            if($usr=="user" and $pwd=="password") {
            session_start();
            $_SESSION['usr'] = $usr;
            header("location:frontpage.php?site=admin");
            exit;
            }
            ?>
            

            🙂

              where is session_start(), you can't manage a session without session_start()

                I see session start in both pages and all examples...

                  you can still use header even after an output, just set your output buffering value 'on' in php.ini. but it will make your web page load slower.
                  i want to ask, any way to check if js is enable or disable on client?

                    You can use <noscript></noscript> and output a different page if no js is enabled

                    More info here

                      Write a Reply...