0

I'm working on a login page and it's working great IF I insert the correct username and password. if not, it will look like this,

leave the username and password blank - blank page login.php appear

correct username, password blank - (working ok, redirect back to index.php)

blank username, wrong password - blank page login.php appear

index.php

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

login.php

<?php 

    include('config.php');

    $connection = mysql_connect("localhost","root","") or die ("Could Not Connect To Server".mysql_error());
    $selection = mysql_select_db("permohonan_data") or die ("Could Not Connect To Database".mysql_error());

    $User = null;
    $Pass = null;
    $username = $_POST['username'];
    $password = $_POST['password'];

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

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

        while($row = mysql_fetch_array($result))
        {   
            $User = $row['username'];
            $Pass = $row['password'];

            if ($password == $Pass)
            {
?>

<script languange='JavaScript'>

    alert("Welcome <?php echo "$username" ?> to admin page! ");

<?php

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

location.href='admin.php';

</script>

<?php
} 
else 
{
?> 

<script language="javascript">

    alert("Ops! Please try again!");

</script>

<script language="javascript">

    location.href="index.php";

</script>

<?php

}

}
?>   

why the other two doesnt redirect back to index.php?

4
  • Can you format your code, please? Commented Apr 1, 2014 at 8:42
  • 1
    Just a remark, don't merge javascript (client-side) and php (server-side). Commented Apr 1, 2014 at 8:42
  • Where does your "if" start? Reformat please :) Commented Apr 1, 2014 at 8:42
  • for login process, you should not use javascript or html redirect, check and redirect in server only. use php headers Commented Apr 1, 2014 at 8:53

2 Answers 2

1

I find your code a bit confusing. To yourself in the first place.

You have index.php, admin.php, make the index.php post on itself

[Code][/code]

And check on the top of index.php if the login is correct. If it is, redirect to admin. Otherwise echo an error message.

And you don't need that much javascript - which can be disabled for some users, and might not work

Sign up to request clarification or add additional context in comments.

Comments

0

As @Debflav already said, you should'nt merge client side with server side,

Your code is split into two entirely separate parts, the server side and the client side.

                    |
               ---------->
              HTTP request
                    |
+--------------+    |    +--------------+
|              |    |    |              |
|    browser   |    |    |  web  server |
| (Javascript) |    |    |  (PHP etc.)  |
|              |    |    |              |
+--------------+    |    +--------------+
                    |
  client side       |      server side
                    |
               <----------
          HTML, CSS, Javascript
                    |

The two sides communicate via HTTP requests and responses. PHP is executed on the server and outputs some HTML and maybe Javascript code which is sent as response to the client where the HTML is interpreted and the Javascript is executed. Once PHP has finished outputting the response, the script ends and nothing will happen on the server until a new HTTP request comes in.

All PHP code is executed on the server before the client even starts executing any of the Javascript. There's no PHP code left in the response that Javascript could interact with.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.