简体   繁体   English

修复php中的错误

[英]Fixing Errors in php

I have been getting the following errors in my php file: 我的php文件中出现以下错误:

Notice: Undefined index: errStr in /clientdata/zeus-dynamic-1/c/r/crowndoor.com.au/www/crowntest/contact.php on line 13 注意:未定义的索引:第13行的/clientdata/zeus-dynamic-1/c/r/crowndoor.com.au/www/crowntest/contact.php中的errStr

Notice: Undefined index: sent in /clientdata/zeus-dynamic-1/c/r/crowndoor.com.au/www/crowntest/contact.php on line 20 注意:未定义的索引:在第20行的/clientdata/zeus-dynamic-1/c/r/crowndoor.com.au/www/crowntest/contact.php中发送

which appear at the top of the browser window. 它出现在浏览器窗口的顶部。 I have tried turning off notices using: error_reporting(E_ALL ^ E_NOTICE); 我尝试使用以下命令关闭通知:error_reporting(E_ALL ^ E_NOTICE); but it doesn't seem to make any difference. 但它似乎没有任何区别。 I am now attempting to fix the undefined indexes. 我现在正在尝试修复未定义的索引。 my php is as follows: 我的php如下:

<?php
session_name("fancyform");
session_start();


$_SESSION['n1'] = rand(1,20);
$_SESSION['n2'] = rand(1,20);
$_SESSION['expect'] = $_SESSION['n1']+$_SESSION['n2'];


$str='';
if($_SESSION['errStr'])
{
    $str='<div class="error">'.$_SESSION['errStr'].'</div>';
    unset($_SESSION['errStr']);
}
if (!isset($_POST['errStr'])) 
{
//If not isset -> set with dumy value 
$_POST['errStr'] = "undefine"; 
}

$success='';
if($_SESSION['sent'])
{
    $success='<h1>Thank you!</h1>';

    $css='<style type="text/css">#contact-form{display:none;}</style>';

    unset($_SESSION['sent']);
}
?>

If anyone has any thoughts about how to fix these notices from appearing that would be great. 如果有人有任何想法如何解决这些通知出现那将是伟大的。

It means the array index you're looking for does not exist. 这意味着您正在寻找的数组索引不存在。 The two ways to handle that are to either make sure it exists, then this error message would be pointing out a logic flaw in your app. 处理它的两种方法是确保它存在,然后此错误消息将指出您的应用程序中的逻辑缺陷。 Or, if the index may legitimately not exist, use isset to check before accessing it. 或者,如果索引可能合法地不存在,请在访问之前使用isset进行检查。 See The Definitive Guide To PHP's isset And empty . 请参阅PHP的权威指南isset并为空

在php页面的顶部添加代码。

<?php error_reporting(0); ?>

你为什么不像使用$_POST['errStr']一样使用isset()

您需要先检查密钥是否存在。

if(isset($_SESSION['errStr']) && $_SESSION['errStr']) {

Change line 13: 改变第13行:

if($_SESSION['errStr'])

To: 至:

if(isset($_SESSION['errStr']))

You can however check manually yourself without interfering the php.ini setting as it will add more complication on the deployment when you migrate your server one day. 但是,您可以自己手动检查而不会干扰php.ini设置,因为当您迁移服务器一天时,它会在部署中添加更多复杂功能。

To check manually, please see the following code...you have done it in a line somewhere in your code though... 要手动检查,请参阅以下代码...您已经在代码中的某个位置完成了它...

<?php
if (isset($_SESSION['sent'])) {
    $success='<h1>Thank you!</h1>';
    $css='<style type="text/css">#contact-form{display:none;}</style>';
        unset($_SESSION['sent']);
}
?>

Turn of notices like: 通知如下:

error_reporting(E_ALL & ~E_NOTICE ) error_reporting(E_ALL & ~E_NOTICE

and/or check the existance of the array keys first. 和/或首先检查数组键的存在。

$sent = array_key_exists('sent', $_SESSION) ? $_SESSION['sent'] : null;

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

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