简体   繁体   English

页面在本地主机上运行良好但未在托管服务器上运行

[英]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.我创建了一个 PHP 网站,它在我的本地主机上运行良好,但是当我在服务器上上传相同的代码时,它在服务器上显示空白页面。

Problem what I suspected is wherever in my code I used a database connection ( dbc.php ) my pages are getting blank.我怀疑的问题是在我的代码中的任何地方我使用了数据库连接( dbc.php )我的页面变得空白。 When I am removing that database connection code it is going fine.当我删除该数据库连接代码时,一切正常。

I am putting login code and dbc.php.我正在输入登录代码和 dbc.php。

My code for login.php我的登录密码.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我的 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 1)我试过这些问题,但没有运作

PHP is not working on server PHP 在服务器上不工作

PHP redirect not working on server PHP 重定向在服务器上不起作用

2) I tried adding error check also, nothing is getting displayed on a webpage, even tried going through cPanel error log. 2) 我也尝试添加错误检查,网页上没有显示任何内容,甚至尝试查看 cPanel 错误日志。

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

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. a) 确保 MySql 服务器已启动并正在运行。
b) Make sure the DB and table you're trying to access were correctly imported. b) 确保您尝试访问的数据库和表已正确导入。
c) Also make sure that you've permissions to access that specific DB. c) 还要确保您有权访问该特定数据库。


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.不,很好,继续添加小段代码,直到你找到造成你生命中浪费的 3 天的原因。

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.尝试以上方法后,我将重复自己,尝试以上方法后如果仍有问题,请在我的答案下方评论,我会尽力帮助您。

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. mysql_real_escape_string函数从 PHP 4.3.0 开始被弃用,将来会被移除。在最新版本的 PHP 中它会抛出警告消息。如果在header函数之前的页面中会打印任何消息(文本/警告/错误),那么它不会重定向。

Might be here it is not allowing to establish the connection with DB.You may use mysqli functions for the same.可能是这里不允许建立与DB的连接。您可以使用mysqli函数进行相同的操作。

I think @Pedro Lobito expalined every possible soultion in a good way , you should consider doing that steps by step as he suggested you.我认为@Pedro Lobito 很好地解释了所有可能的问题,您应该考虑按照他的建议逐步执行此操作。

But for now you can try editing your但现在你可以尝试编辑你的

login.php登录.php

like this i hope this will help but try @Pedro answer also .像这样,我希望这会有所帮助,但也尝试@Pedro 回答。

<?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.欢迎使用 Stackoverflow。

    //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");
?>

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.检查您是否正确加载了所有cssjs文件。 Make sure.确保。

  2. Remove comments from css files.css文件中删除注释。 Very important.很重要。

  3. Update jquery.更新 jquery。 Use the CDN ones if possible.如果可能,请使用 CDN。 Don't use slim as some of the function will throw an error.不要使用 slim 因为某些函数会抛出错误。

  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检查您的 php 文件是否有错误将以下内容放在您的标题 php 标签中

    ni_set('display_errors', 1); ni_set('display_errors', 1); ini_set('display_startup_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);错误报告(E_ALL);

For my case it was jquery file not working and comments in css file.就我而言,它是 jquery 文件不起作用和 css 文件中的注释。 Also loading files that are not in the directory eg saying href="assets/css/main.css" while it's not there.还加载不在目录中的文件,例如说 href="assets/css/main.css" 而它不在那里。

If my understand of your problem correct.如果我对你的问题的理解正确。 First thing you have to edit dbc.php.首先,您必须编辑 dbc.php。 Because username, password and databasename is not the same value as in "Localhost"(XAMPP).因为用户名、密码和数据库名称与“本地主机”(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".当您在您的域中创建 mysqldatabase 时,它会要求您使用“强密码”创建“用户”,并且不能为“空白”。 So, you have to put "correct value" in your dbc.php with "the same value" in your domain/sub domain settings.因此,您必须在 dbc.php 中输入“正确的值”,并在域/子域设置中输入“相同的值”。 If this is already done, then we can go to the next track of issue.如果这已经完成,那么我们可以 go 进行下一个问题跟踪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM