【问题标题】:Having two undefined index (email and code, probably because of my htaccess file)有两个未定义的索引(电子邮件和代码,可能是因为我的 htaccess 文件)
【发布时间】:2014-08-08 15:00:12
【问题描述】:

我正在做一个时事通讯系统,用户在我的输入文本中输入他的邮件,然后单击提交以订阅时事通讯。

点击提交后,他会收到一封电子邮件,说要确认他的订阅,他需要点击我在这封电子邮件中提供的链接。

这是链接:

<p>To confirm your subscription click on link below:</p>
    <a href="http://localhost/website/newsletter/confirm?email='.$email.'&amp;code='.$code.'">Confirm Subscription</a>

此链接将重定向到我的时事通讯确认页面,我将在其中收到电子邮件和代码,然后我将在我的订阅者表上进行更新。

$email = $_GET['email'];
$code= $_GET['code'];
$pdo = start();
$updSub = $pdo->prepare("UPDATE subscribers set status= ? WHERE code = ?");
$updSub->bindParam(2,$code);
$updSub->execute();

但我有这两个通知:

注意:未定义索引:F:\Xampp\htdocs\website\newsletter\confirm.php 中的电子邮件

注意:未定义索引:F:\Xampp\htdocs\website\newsletter\confirm.php 中的代码

您知道为什么会发生这种情况吗?我使用的是 .htaccess 文件,不知道是不是因为这个,在 url 中传递变量代码和电子邮件时出现了一些问题。

这是我的 htaccess 文件:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

我的查询字符串:

@$url = $_GET['url'];
$url = explode('/', $url);
$url[0] = ($url[0] == NULL ? 'index' : $url[0]);

if(file_exists('template/'.$url[0].'.php')){
    require_once('template/'.$url[0].'.php');
}elseif(@file_exists('template/'.$url[0].'/'.$url[1].'.php')){
    require_once('template/'.$url[0].'/'.$url[1].'.php');
}
else{
    require_once('template/404.php');
}

【问题讨论】:

  • 请在您的 htaccess 中显示规则(mod-rewrite)
  • 我用我的 htaccess 文件更新了我的问题!
  • 您可以像这样修复未定义的索引:$email = isset($_GET['email']) ? $_GET['email'] : '';$code= isset($_GET['code']) ? $_GET['code'] : '';
  • 谢谢。我知道。但是我在一段代码中有这两个未定义的索引,它们应该已经存在。
  • 这个问题被错误地标记为重复。这与经典的未定义索引无关

标签: php .htaccess email


【解决方案1】:

首先,当您处理用户的输入并直接放入 URL 时,请确保您对它进行 URL 编码。即使E-Mail地址不能有空格等。

如果您使用 JavaScript 来处理,请使用:

encodeURIComponent("");
encodeURIComponent(document.getElementById('email').value);
encodeURIComponent($("#email").val());  // jQuery Library Required

如果您在没有 javascript 干预的情况下使用表单来处理用户输入,则不必担心 URL 编码,这应该会自动发生。

如果使用 PHP 处理输入 -> URL,请使用:

rawurlencode($email); 

之后,undefined index 发生在您调用一个变量而没有事先声明它时。就像在说:

echo $apples;  // where $apples has not been previously declared

现在,检查所有 includerequire 文件,确保您没有在某处调用相同的变量名。

使用issetempty 之类的函数将为您提供更好的验证流程。

if(!empty($_GET['email'])){
     mysqli->real_escape_string($email = $_GET['email']);
}else{
     die("No E-Mail");
}

并且将停止未定义的索引错误,前提是您正确检查以确保变量存在。

简单明了,您的问题是因为您在声明变量之前要求它们。如果它们的值为 NULL,则不会发生此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-26
    • 2016-05-19
    • 2017-10-23
    • 2019-05-21
    • 2020-01-27
    • 2020-04-24
    • 2022-07-20
    • 2014-09-25
    相关资源
    最近更新 更多