简体   繁体   中英

loging php page not redirecting

I'm working on a login page by user level to separate the admin and user. but it didnt seems to work. it doesnt redirect and leave a blank page. I've tried remove the javascript part, but it doesnt change anything either.

index.php

<form class="login" action="login.php" method="post">
Username:<input type="text" name="username" id="username"/>
Password:<input type="password" name="password" id="password"/>
<input type="submit" value="login"/>
</form>

login.php

<?php 
    session_start();
    include('config.php');

    if(isset($_POST['submit'])) {

        $username = $_POST['username'];
        $password = $_POST['password'];

            $username = mysql_real_escape_string($username);    
            $password = mysql_real_escape_string($password);

            $sql = mysql_query("SELECT * FROM admin WHERE username='$username' AND password='$password'");
            $result = mysql_fetch_array($sql);

            $username=$result['username'];
    $adminID=$result['adminID'];
    $userLevel=$result['UserLevel'];

    $_SESSION['adminID']=$adminID;
    $_SESSION['userLevel']=$userLevel;
    $_SESSION['username']=$username;
    $_SESSION['password']=$password;

    if($userLevel == '1')
    {
     $sql = "UPDATE admin SET status = 'AKTIF' where username = '$username' ";
     $result = mysql_query($sql) or die('Cannot UPDATE.'.mysql_error());
    ?>

    <script type="text/javascript">
        alert("Welcome <?php echo "$username" ?> to Admin page! ");
    </script>

    <?php
        header('Location:admin.php');
        exit();
    }

    elseif($userLevel == '0')
    {
    $sql = "UPDATE admin SET status = 'AKTIF' where username = '$username' ";
    $result = mysql_query($sql) or die('Cannot UPDATE.'.mysql_error());
    ?>

    <script type="text/javascript">
        alert("Welcome <?php echo "$username" ?> to User page! ");
    </script>

    <?php
        header('Location: user.php');
        exit();
    }

    else
    {   
    ?>
       <script type="text/javascript">
        alert("Invalid Username or Password! ");
        //window.location.href = "index.php";
    </script>

    <?php
    }
}

?>

Use PHP Header:

for userLevel1:
header("Location: admin.php");

for userLevel2:
header("Location: user.php");

Name in your submit so it will enter your PHP code block:

<input type="submit" name="submit" value="login"/>

try the following code and replace into your code. see whether can work or not. you try on the first if condition first and see on the result. if cannot work tell me what problem you face.

<?php
    if($userLevel == '1')
                 $sql = "UPDATE admin SET status = 'AKTIF' where username = '$username' ";
                 $result = mysql_query($sql) or die('Cannot UPDATE.'.mysql_error());
    ?>
    <script>
                var a = alert("Welcome <?php echo "$username" ?> to Admin page! ");
    if (a === true){
    window.location.href="admin.php";
    }
    else{
    window.location.href="admin.php";
    }
</script>
    <?php
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM