有两个未定义的索引(电子邮件和代码,可能是因为我的htaccess文件)


Having two undefined index (email and code, probably because of my htaccess file)

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

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

这是链接:

<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');
}

首先,当您处理用户的输入并直接放置到URL中时,请确保您对其进行了URL编码。即使电子邮件地址不能有空格,等等。

如果你使用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,则不会发生此问题。