1

I'm making a simple login script in PHP. Its working fine on my XAMPP localhost but I can not run it on CPANEL. The scarier part is it does not give me any error instead the login form page reload every time I submit.

THE FORM:

<div class="panel-body">
    <form role="form" method="post" action="<?php $_PHP_SELF ?>">
      <div class="form-group">
        <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
          <input type="text" class="form-control" id="exampleInputEmail1" name="name" placeholder="Admin ID" required>
        </div>
      </div>
      <div class="form-group">
        <div class="input-group"> <span class="input-group-addon"><span class="glyphicon glyphicon-star"></span></span>
          <input type="password" class="form-control" id="exampleInputPassword" name="password" placeholder="Password" required>
        </div>
      </div>
       <hr/>
   <!--   <button type="button" class="btn btn-success"><span class="glyphicon glyphicon-arrow-left"></span> Back</button>-->
      <button type="submit" class="btn btn-warning" name="admin_login"><span class="glyphicon glyphicon-log-in"></span> Sign In</button>
      <p><br/>
      </p>

THE SCRIPT on the same php file:

<?php
include "sessionforadmin.php";
include "connection.php";

if(isset($_POST["admin_login"])){

if(!empty($_POST['name']) && !empty($_POST['password'])) {
    $user=$_POST['name'];
    $pass=$_POST['password'];

    $user = mysql_real_escape_string($user);
    $pass= md5( mysql_real_escape_string($pass));

    $query=mysql_query("SELECT * FROM admin WHERE admin_name='".$user."' AND password='".$pass."'");
    $numrows=mysql_num_rows($query);

    if($numrows!=0)
    {
    while($row=mysql_fetch_assoc($query))
    {
    $dbusername=$row['admin_name'];
    $dbpassword=$row['password'];
    }

    if($user == $dbusername && $pass == $dbpassword)
    {

    user_login( $user );

    /* Redirect browser */
    header("Location: home.php");
    }
    } else {
    ?>
    <script>
        alert("Invalid username or password!");
        document.location.href='index.php';
        </script>
        <?php 
    }

} else { ?>
    <script>
        alert("All Field are required!");
        document.location.href='index.php';
        </script>
        <?php 
}
}

?>

EDIT :

The Error that I receive on doing error_reporting is:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/exploitb/public_html/badminpanel/index.php:29) in /home/exploitb/public_html/badminpanel/sessionforadmin.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/exploitb/public_html/badminpanel/index.php:29) in /home/exploitb/public_html/badminpanel/sessionforadmin.php on line 2 Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/exploitb/public_html/badminpanel/connection.php on line 8

Where am I wrong here??

Nehal
  • 1,542
  • 4
  • 17
  • 30
ZIS
  • 507
  • 1
  • 6
  • 14
  • try echo in if and else and then exit(); in script to check where your script is going –  Feb 03 '16 at 04:45
  • Same PHP versions? Did you check the error logs? If running PHP 7 the `mysql_` functions wont work because they no longer exist. – chris85 Feb 03 '16 at 04:46
  • @DivyeshSavaliya I tried, I am not getting any echo. Page just reloaded. – ZIS Feb 03 '16 at 04:50
  • have you added exit(); after echo? –  Feb 03 '16 at 04:51
  • @DivyeshSavaliya YES. – ZIS Feb 03 '16 at 05:00
  • still page is reloaded that means compiler does't reach at that point. try echo in first if and then exit() –  Feb 03 '16 at 05:01
  • @DivyeshSavaliya Now it is giving me server error 500. – ZIS Feb 03 '16 at 05:06
  • Error gone if you remove echo and exit()? –  Feb 03 '16 at 05:08
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102424/discussion-between-zahidul-islam-suzon-and-divyesh-savaliya). – ZIS Feb 03 '16 at 05:12
  • put this code on the top of your page error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); – Nehal Feb 03 '16 at 05:59
  • Yes, now it is showing 3 warning. – ZIS Feb 03 '16 at 06:11
  • Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/exploitb/public_html/badminpanel/index.php:29) in /home/exploitb/public_html/badminpanel/sessionforadmin.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/exploitb/public_html/badminpanel/index.php:29) in /home/exploitb/public_html/badminpanel/sessionforadmin.php on line 2 Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/exploitb/public_html/badminpanel/connection.php on line 8 – ZIS Feb 03 '16 at 06:11
  • Got your error, since you are selecting DB using `mysqli_select_db`, and you are selecting queries using `mysql_`. Try using any one of these. I would suggest you to use `mysqli_` as `mysql_` is deprecated – Nehal Feb 03 '16 at 06:53
  • @ZahidulIslamSuzon, please show the content of your connection file as well – Nehal Feb 03 '16 at 06:56

2 Answers2

0

by default in the cpanel(online host) might disable error reporting, put error_reporting(E_ALL); beginning of the php code then you might can see where you wrong

Supun Praneeth
  • 3,087
  • 2
  • 30
  • 33
  • Nothing. Except reloading! – ZIS Feb 03 '16 at 05:01
  • then you have to debug the code, for that first put `exit();` after the `include "connection.php";` still reloading ? – Supun Praneeth Feb 03 '16 at 05:06
  • then you have something wrong with these two files`sessionforadmin.php` `connection.php` – Supun Praneeth Feb 03 '16 at 05:19
  • //sessionforadmin.php – ZIS Feb 03 '16 at 05:24
  • And the connection.php is Ok as I can retrieve information from database on main site. – ZIS Feb 03 '16 at 05:27
  • plz check if any browser output before ther `header` or `session_start();` read this http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Supun Praneeth Feb 03 '16 at 05:44
  • Ok! Anyway, is this something to do with the host provider? – ZIS Feb 03 '16 at 05:49
  • Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/exploitb/public_html/badminpanel/index.php:29) in /home/exploitb/public_html/badminpanel/sessionforadmin.php on line 2 Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /home/exploitb/public_html/badminpanel/index.php:29) in /home/exploitb/public_html/badminpanel/sessionforadmin.php on line 2 Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/exploitb/public_html/badminpanel/connection.php on line 8 – ZIS Feb 03 '16 at 06:14
  • yeah its do with the host provider, it becouse in your local host in the apache by default ob buffer is enable but not in the online host. so when u using header ,session_start ,setcookie you can't out put to browser anything before those method.read this ypu will getbthe idea http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php – Supun Praneeth Feb 03 '16 at 06:25
  • Solve the problem simply configuring the php.ini setting to default. Thank you though :) – ZIS Feb 03 '16 at 07:54
0

Instead of using mysql_query(), use mysqli_query();

Syntax: mysqli_query(your_connection_variable, sql_query);

Debanjan Roy
  • 21
  • 1
  • 8