2

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);
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Gaurav Dubey
  • 41
  • 1
  • 4

6 Answers6

3

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.

Community
  • 1
  • 1
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • @Pedro Lobito I checked the first mistake pointed out by you.I couldn't find any probelm in that piece of code.Firstly I am checking if the the two fields are filled or not.If filled I retrieve its entry from database to check if its present in the database. If entry is already present user is allowed to login else not. Could you please elaborate the mistake as pointed out by you. – Gaurav Dubey Apr 20 '15 at 18:01
  • Part 1: The user can type anything for `telephone` and `password`, you only check if the field is empty. This is a problem but for now not your biggest one. In part 3: have you tried to add more code ? if so, did you get any errors? – Pedro Lobito Apr 20 '15 at 21:57
  • Explain how it is a problem? The entire point is to allow the user to type ANYTHING for telephone and password EXCEPT an empty field, when both fields are present, first then you have values that you can work and interact with to verify the user. Your statement (First issue), even tho i wouldn't use it exactly like he does with '', is completely wrong and he's doing exactly what he's supposed to. –  Feb 27 '17 at 08:35
0

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.

0

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.

twister_void
  • 1,377
  • 3
  • 13
  • 31
0
    //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");
?>
0

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.

Kipruto
  • 721
  • 6
  • 16
0

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.

DharmanBot
  • 1,066
  • 2
  • 6
  • 10
Edwin M
  • 21
  • 3