'Pages are working fine on localhost but not running on the hosting server

I created a PHP website which is working fine on my localhost but when I uploaded the same code on the server, it is showing me blank pages on the server.

Problem what I suspected is wherever in my code I used a database connection (dbc.php) my pages are getting blank. When I am removing that database connection code it is going fine .

I am putting login code and dbc.php.

My code for login.php

<?php


include('dbc.php');


 // get form data, making sure it is valid
 $phone = mysql_real_escape_string(htmlspecialchars($_POST['id']));
 $pass = mysql_real_escape_string(htmlspecialchars($_POST['password']));

 // check to make sure both fields are entered
 if ($phone == '' || $pass == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
header("Location: " .$_SERVER['HTTP_REFERER']);
 } 
 else
 {
 // save the data to the database

    $login_sql=mysql_query("select * from member where email='$phone' and  password='$pass'")
    or die(mysql_error()); 
    $num=mysql_num_rows($login_sql);

    if($num>0)
    {
     session_start();
     $admin_data=mysql_fetch_array($login_sql);
     $_SESSION['usermatri_id']=$admin_data['mid'];


        if($admin_data['mid']>0)
            {
                 header("Location: dashboard.php"); 

            }
        else 
            {
                header("location:".$_SERVER["HTTP_REFERER"]);
            }

     exit;
     }
 else{

    header("location:".$_SERVER["HTTP_REFERER"]);
 }
 }



  // if the form hasn't been submitted, display the form


?>

My Code for dbc.php

<?php
//connection to the database
mysql_connect("localhost","root","")or die(mysql_error('cannot connect'));
mysql_select_db("matrimony");
?>

1) I tried these questions, but not functioning

PHP is not working on server

PHP redirect not working on server

2) I tried adding error check also, nothing is getting displayed on a webpage, even tried going through cPanel error log .

ini_set('display_errors',1); 
 error_reporting(E_ALL);


Solution 1:[1]

I think @Pedro Lobito expalined every possible soultion in a good way , you should consider doing that steps by step as he suggested you.

But for now you can try editing your

login.php

like this i hope this will help but try @Pedro answer also .

<?php
 $mail = $_POST['id'];
 $pass = $_POST['password'];
// check to make sure both fields are entered
 if ($mail == '' || $pass == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
header("Location: " .$_SERVER['HTTP_REFERER']);
 } 
else 

{
         include('dbc.php');
         $sql=mysql_query("SELECT * from member where email='$mail' and  password='$pass'")or die(mysql_error());
         $num=mysql_num_rows($sql);
         if($num>0)
     {
               $admin_data=mysql_fetch_array($sql);
           session_start();
           $_SESSION['usermatri_id']=$admin_data['mid'];

           header("Location: dashboard.php");    

        }
     else 
     {
        header('Location:' . $_SERVER['PHP_SELF']);
     }


}

?>

Welcome to Stackoverflow.

Solution 2:[2]

First

The code below is way, way wrong...

 if ($phone == '' || $pass == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 // if either field is blank, display the form again
header("Location: " .$_SERVER['HTTP_REFERER']);
 } else{
//the juice...
}

the above means that if :

$_POST['id'] = 123456;
$_POST['password'] = 123456;

I will still be able to drink the "Juice"


Second:

a) Make sure MySql server is up and running.
b) Make sure the DB and table you're trying to access were correctly imported.
c) Also make sure that you've permissions to access that specific DB.


Third:

Start small

Just add the following to your script:

ini_set('display_errors',1); 
error_reporting(E_ALL);

mysql_connect("localhost","root","")or die(mysql_error('cannot connect'));
mysql_select_db("matrimony");

Any errors ? no, good, continue adding small pieces of code until you find the responsible for 3 wasted days of your life.

After trying the above, I'm going to repeat myself, after trying the above if you still have problems, comment below my answer, I'll try to help you further.

Solution 3:[3]

mysql_real_escape_string function was deprecated from PHP 4.3.0, and will be removed in the future.In the latest version of PHP it is throwing a warning message.If any message(text/warning/error) will print in the page before header function,then it will not redirect.

Might be here it is not allowing to establish the connection with DB.You may use mysqli functions for the same.

Solution 4:[4]

    //index.php
<?php
$conn = mysqli_connect("localhost","root","","md5login");
session_start();

if (isset($_SESSION["Email"]))
{
    header("location:welcome.php");
}
if (isset($_POST["register"]))
{
    if (empty($_POST["Email"]) && empty($_POST["Password"]))
    {
        echo '<script>alert("Both Field Are Required")</script>';
    }
    else
        {
        $Email = mysqli_real_escape_string($conn, $_POST["Email"]);
        $Password = mysqli_real_escape_string($conn, $_POST["Password"]);
        $Password = md5($Password);
        $query = "INSERT INTO client (Email, Password) VALUES('$Email','$Password')";
        if (mysqli_query($conn, $query))
        {
            echo '<script>alert("Rgistration Done")</script>';
        }
    }
}
    if (isset($_POST["login"]))
    {
        if (empty($_POST["Email"]) && empty($_POST["Password"]))
        {
          echo '<script>alert("Both r Required")</script>';
        }
        else
        {
            $Email = mysqli_real_escape_string($conn, $_POST["Email"]);
            $Password = mysqli_real_escape_string($conn, $_POST["Password"]);
            $Password = md5($Password);

            $query = "SELECT * FROM client WHERE Email = '$Email' AND Password = '$Password'";
            $result = mysqli_query($conn, $query);

            if (mysqli_num_rows($result) > 0)
            {
                $_SESSION['Email'] = $Email;
                header("location:welcome.php");

            }
            else
            {
                echo '<script>alert("Wrong User Details")</script>';
            }
        }
    }

?>
<html>
<head>
    <title>Login And Registeration</title>
</head>
<body>
<div class="container" style="width:500px;">
<h3 align="center"> Login And Register </h3>
    <br>
    <?php
    if (isset($_GET["action"]) == "login")
    {
    ?>
    <h3 align="center">Login</h3>
    <br>
        <form method="post">
            <label>Enter Email</label>
            <input type="text" name="Email">
            <br/>
            <label>Enter Password</label>
            <input type="text" name="Password">
            <br/>
            <input type="submit" name="login" value="Login">
            <br/>
            <p align="center"><a href="index.php">Register</a></p>
        </form>
    <?php
    }
    else
    {
    ?>
        <h3 align="center">Register</h3>
        <br>
        <form method="post">
            <label>Enter Email</label>
            <input type="text" name="Email">
            <br/>
            <label>Enter Password</label>
            <input type="text" name="Password">
            <br/>
            <input type="submit" name="register" value="register">
            <br/>
            <p align="center"><a href="index.php?action=login">Login</a></p>
        </form>
    <?php
    }
    ?>


</div>
</body>
</html>

//welcome.php
<?php
session_start();
if (!isset($_SESSION["Email"]))
{
    header("location:index.php?action=login");
}
?>
<html>
<head>
    <title>Welcome Page</title>
</head>
<body>
<?php
echo '<h2>Welcome - '.$_SESSION["Email"].'</h2>';
echo '<a href="logout.php">Logout </a>';
?>

</body>
</html>

//logout.php

<?php
session_start();
session_destroy();
header("location:index.php?action=login");
?>

Solution 5:[5]

I had this issue and I almost shed tears. Client was waiting on me while I had promised to share a link. Now to fix this there are a number of troubleshooting you'll do:

  1. Check if you are loading all css and js files correctly. Make sure.

  2. Remove comments from css files. Very important.

  3. Update jquery. Use the CDN ones if possible. Don't use slim as some of the function will throw an error.

  4. Check Database connection if password and all that are correct.

  5. Check your php files if they have errors put the following in your header php tag

    ni_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

For my case it was jquery file not working and comments in css file. Also loading files that are not in the directory e.g saying href="assets/css/main.css" while it's not there.

Solution 6:[6]

If my understand of your problem correct. First thing you have to edit dbc.php. Because username, password and databasename is not the same value as in "Localhost"(XAMPP). This is Might be the reason in website blank, but in localhost working fine.

When you create mysqldatabase in your domain, it will ask you to create "User" with "Strong Password" and it can not be "Blank". So, you have to put "correct value" in your dbc.php with "the same value" in your domain/sub domain settings. If this is already done, then we can go to the next track of issue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 twister_void
Solution 2 Community
Solution 3 Pravat Kumar Sahoo
Solution 4
Solution 5 Kipruto
Solution 6 DharmanBot