Index.php 在实时网站上导致空警报
Posted
技术标签:
【中文标题】Index.php 在实时网站上导致空警报【英文标题】:Index.php causing empty alert on live website 【发布时间】:2016-05-01 08:17:01 【问题描述】:所以我有 index.php 有我的默认页面。它在 xampp 上运行良好。因此,我将整个网站上传到 1&1(我的域/托管服务提供商),当我尝试访问我的域时,我收到了一个没有消息的空警报和一个完全空白的页面。
我将文件名更改为 index.html 并且网页加载正常。所以我知道它必须是 .php 扩展名或我的代码。
我还添加了一个名为 .htaccess 的文件,它只包含:
DirectoryIndex index.php
这是我在 index.php 顶部的 php 代码(将敏感信息替换为 *s):
<?php
//Connect to a database
$host_name = "******.db.1and1.com";
$database = "db****";
$user_name = "dbo******";
$password = "***z.0**";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
// echo("nice job");
//Take the values from the html form and assign them to variables
$ID = $_POST['name'];
$userpassword = $_POST['password'];
//If no passsowrd entered then go straight to index.php
echo "<script type='text/javascript'>alert($userpassword);</script>";
if ($userpassword == null)
header("Location: http://localhost:82/index3.php");
die();
//Check to see if the password matches the hashes
if (md5($userpassword) === '******************'
or md5($userpassword) === '***********'
or md5($userpassword) === '****************'
or md5($userpassword) === '**************')
//Add the visitor name to our list
mysqli_query($connect, "INSERT INTO `WebsiteVisitors` (`Name`) VALUES ('$ID')") or die("Error in INSERT: ".mysqli_error($connect));
// echo "You have entered the correct password, congrats.";
// Start the session so they can access other pages
session_start();
$_SESSION['loggedIn'] = true;
// Redirect them to rest of site
header("Location: http://localhost:82/home.php");
die();
else
header("Refresh: 0; url=index2.php");
echo "<script type='text/javascript'>alert(\"Wrong Password. Check your invitation card.\");</script>";
?>
【问题讨论】:
白屏死机:错误检查\显示已关闭,打开它们以查看错误。在您的 php 页面顶部添加:ini_set('display_errors', 'On'); ini_set('html_errors', 0); error_reporting(-1);
你有一个错误,所以你得到空白页。尝试启用 php 错误
这是我得到的错误:Notice: Undefined index: name in /homepages/32/d563934655/htdocs/directory2/index.php on line 15 Notice: Undefined index: password in /homepages/32/d563934655/htdocs/directory/index.php on line 16 Warning: Cannot modify header information - headers already sent by (output started at /homepages/32/d563934655/htdocs/sandandmel/index.php:1) in /homepages/32/d563934655/htdocs/sandandmel/index.php on line 21
所以'name'是登录字段的名称。我的 index.php 是一个要求输入名称和密码的登录页面。
【参考方案1】:
由于 $_POST
请求仅在您的情况下提交表单后出现,因此您只需在 $_POST["name"]
和 $_POST["password"]
存在时执行用户名和密码检查。
所以在使用和操作$_POST
变量之前给出一个if 语句if(isset($_POST['name']) && isset($_POST['password']))
。此外,session_start()
应在脚本顶部给出。
以下是您的完整代码,包括检查
<?php
session_start();
// session start should be at top of your script
error_reporting(E_ERROR); // reports only errors
//Connect to a database
$host_name = "******.db.1and1.com";
$database = "db****";
$user_name = "dbo******";
$password = "***z.0**";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
// $_POST request comes only when form is submitted in your case. So check for $_POST['name'] and $_POST['password']
if(isset($_POST['name']) && isset($_POST['password']))
$ID = $_POST['name'];
$userpassword = $_POST['password'];
//If no passsowrd entered then go straight to index.php
if ($userpassword == null)
echo "<script type='text/javascript'>alert("Empty Password");</script>";
header("Location: http://localhost:82/index3.php");
die();
//Check to see if the password matches the hashes
if (md5($userpassword) === '******************'
or md5($userpassword) === '***********'
or md5($userpassword) === '****************'
or md5($userpassword) === '**************')
//Add the visitor name to our list
mysqli_query($connect, "INSERT INTO `WebsiteVisitors` (`Name`) VALUES ('$ID')") or die("Error in INSERT: ".mysqli_error($connect));
$_SESSION['loggedIn'] = true;
// Redirect them to rest of site
header("Location: http://localhost:82/home.php");
die();
else
echo "<script type='text/javascript'>alert(\"Wrong Password. Check your invitation card.\");</script>";
header("Refresh: 0; url=index2.php");
?>
【讨论】:
感谢您的回答,它似乎解决了这个问题。您能否将其清理干净,以便其他人能够发现您添加了isset
,因此代码仅在填写表单时运行。
@Badrush 编辑了我的代码。将此答案标记为已接受,以便其他人可以轻松找到解决方案
你能在代码外加一条信息突出显示添加的代码吗,你的cmets和我的cmets在代码中很难区分。我也收到此错误:Warning: session_start(): Cannot send session cache limiter - headers already sent...
这是一个警告。添加error_reporting(E_ERROR);在 session_start() 之后;。添加在我的代码中
您似乎完全删除了会话开始?以上是关于Index.php 在实时网站上导致空警报的主要内容,如果未能解决你的问题,请参考以下文章