0

I have a problem with my code but after submit with username and password, I get blank white page, I tried to make a login system, but there is a problem, when the form is submitted, which appeared just a blank white page

below my script with name login.php

<?php session_start();?>
<!DOCTYPE html>
<html lang="en">
<title>Login</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style>
        body {
            background-color:#eee;
        }
        .row {
            margin:100px auto;
            width:300px;
            text-align:center;

        }
        .login {
            border-radius: 5px;
            background-color:#fff;
            padding:20px;
            margin-top:20px;
        }
    </style>
<body>
<div class="container">
        <div class="row">
<?php
if(isset($_POST['submit'])){
include 'db.php';
$user=addslashes($_POST['user']);
$pass=md5($_POST['pass']);
$cari= "select user from guru where user='$user' and pass='$pass'";
$query= mysql_query($cari);
$num=mysql_num_rows($query);
if($num>0){ 
session_register('user');
$row= mysql_fetch_array($query);
$_SESSION['user']=$row['user'];
header("location:index.php");
exit;
 } else {
 echo '
 <div class="alert alert-danger" role="alert">User atau password salah.</div>';
};
}
?>
<div class="login">

<div class="panel panel-info" >
                    <div class="panel-heading">
                        <div class="panel-title">Masuk</div>

                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                        <form id="loginform" class="form-horizontal" role="form" action="" method="post">

                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                        <input id="login-username" type="text" class="form-control" name="user" value="" placeholder="username">                                        
                                    </div>

                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                        <input id="login-password" type="password" class="form-control" name="pass" placeholder="password">
                                    </div>






                                <div style="margin-top:10px" class="form-group">
                                    <!-- Button -->

                                    <div class="col-sm-12 controls">
                                      <input type="submit" name="submit" class="btn btn-primary btn-block" value="Masuk" />


                                    </div>
                                </div>



                            </form>     



                        </div>                     
                    </div>


    </div>


</body>
</html>
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Check this link http://stackoverflow.com/questions/41234137/how-to-hide-form-when-sending-on-the-same-page/41235353#41235353 – PHP Ninja Jan 18 '17 at 07:07
  • you are having exit; in your code which can be possible reason of your script getting stopped from executing ahead. – Sandeep Garg Jan 18 '17 at 07:30

2 Answers2

0

It is recommended to use either the mysqli or PDO_MySQL extensions. It is not recommended to use the old mysql extension for new development, as it was deprecated in PHP 5.5.0 and was removed in PHP 7. A detailed feature comparison matrix is provided below. The overall performance of all three extensions is considered to be about the same. Although the performance of the extension contributes only a fraction of the total run time of a PHP web request. Often, the impact is as low as 0.1%

Your code should be:

<?php session_start();?>
<!DOCTYPE html>
<html lang="en">
<title>Login</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style>
        body {
            background-color:#eee;
        }
        .row {
            margin:100px auto;
            width:300px;
            text-align:center;

        }
        .login {
            border-radius: 5px;
            background-color:#fff;
            padding:20px;
            margin-top:20px;
        }
    </style>
<body>
<div class="container">
        <div class="row">
<?php
if(isset($_POST['submit'])){
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//to get all error 

//include 'db.php' file is unreachable;
$user=addslashes($_POST['user']);
$pass=md5($_POST['pass']);

    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

    if ($result = mysqli_query($link, "select user from guru where user='$user' and pass='$pass'")) {    

        $num=mysqli_num_rows($result);
        if($num>0){ 
session_register('user');
$_SESSION['user']=$result[0]['user'];
  /* free result set */
    mysqli_free_result($result);
header("location:index.php");
exit;
 } else {
 echo '
 <div class="alert alert-danger" role="alert">User atau password salah.</div>';
};
 }
}
?>
<div class="login">

<div class="panel panel-info" >
                    <div class="panel-heading">
                        <div class="panel-title">Masuk</div>

                    </div>     

                    <div style="padding-top:30px" class="panel-body" >

                        <div style="display:none" id="login-alert" class="alert alert-danger col-sm-12"></div>

                        <form id="loginform" class="form-horizontal" role="form" action="" method="post">

                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
                                        <input id="login-username" type="text" class="form-control" name="user" value="" placeholder="username">                                        
                                    </div>

                            <div style="margin-bottom: 25px" class="input-group">
                                        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
                                        <input id="login-password" type="password" class="form-control" name="pass" placeholder="password">
                                    </div>






                                <div style="margin-top:10px" class="form-group">
                                    <!-- Button -->

                                    <div class="col-sm-12 controls">
                                      <input type="submit" name="submit" class="btn btn-primary btn-block" value="Masuk" />


                                    </div>
                                </div>



                            </form>     



                        </div>                     
                    </div>


    </div>


</body>
</html>

check link

Community
  • 1
  • 1
Abhijit Jagtap
  • 2,740
  • 2
  • 29
  • 43
0

You have to add ob_start() after start_session()

As you are writing html code before you redirect page after successful login header will not redirect page as your header is already send.

ob_start will work as buffer to store html code and will render html code/send header if you explicitly send it or end of file

Roman
  • 2,530
  • 2
  • 27
  • 50
Vaibhav Malve
  • 116
  • 1
  • 9