Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
电子邮件确认链接在PHP中不起作用_Php_Wordpress_Confirmation_Email Confirmation - Fatal编程技术网

电子邮件确认链接在PHP中不起作用

电子邮件确认链接在PHP中不起作用,php,wordpress,confirmation,email-confirmation,Php,Wordpress,Confirmation,Email Confirmation,我正在制作一个带有电子邮件确认的自定义页面模板。一切正常,但当我点击电子邮件中的链接时,它会给我“找不到页面” PHP脚本中有什么问题 以下是片段: ob_start(); $email= ''; if(isset($_POST['submit'])){ if(!($_POST['subscriptions']=="")){ $email= isset($_POST['subscriptions']) ? $_POST['subscriptions'] : ''; function

我正在制作一个带有电子邮件确认的自定义页面模板。一切正常,但当我点击电子邮件中的链接时,它会给我“找不到页面”

PHP脚本中有什么问题

以下是片段:

ob_start();
$email= '';

if(isset($_POST['submit'])){
if(!($_POST['subscriptions']=="")){
    $email= isset($_POST['subscriptions']) ? $_POST['subscriptions'] : '';

function encryptLink($stringValue){
    $key = "12345";
    $qryStr = "subscriptions=".$stringValue;
    $query = base64_encode(urlencode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $qryStr, MCRYPT_MODE_CBC, md5(md5($key)))));
    $link = 'www.example.com/testEnvironment/confirmation/'.$query;

    return $link;
    }
    $string = $email;
    $pagelink = encryptLink($string);

    //Email Details
    $mail_to = $email;
    $from_mail = "dummyemail@example.com";
    $from_name = "";
    $reply_to = "";
    $subject = "Confirmation";
    $message_body =
'Hey,

<span style="overflow-wrap: break-word; word-wrap: break-word;">'.$pagelink.'</span>';

    //Generate a boundary
    $boundary = md5(uniqid(time()));

    //Email Header
    $header = "From: ".$from_mail." \r\n";
    $header .= "Reply-To: ".$reply_to."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed;\r\n";
    $header .= " boundary=\"".$boundary."\"";

    //Multipart wraps the Email Content
    $message_body .= "\r\n\r\n";
    $message_body  .= "--".$boundary."\r\n";
    $message_body  .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n";
    $message_body .= "Content-Transfer-Encoding: 7bit\r\n";
    $message_body  .= "\r\n";
    $message_body .= "$message_body\r\n";
    $message_body .= "--".$boundary."\r\n";

        //Send email
        if (mail($mail_to, $subject, $message_body, $header)){
            echo "Sent";
        } else {
            echo "Error";
        }
    }
}

您正在将密钥与确认url直接连接。它将始终导致404错误页面,因为链接不存在。 相反,您可以通过get请求获取查询,并在确认脚本中进行如下处理

$link = 'www.example.com/confirmation/?key='.$query;
$key=$_GET['key'];
在确认脚本中,通过get方法获取查询,如下所示

$link = 'www.example.com/confirmation/?key='.$query;
$key=$_GET['key'];
将原始密钥存储为
$original\u key=12345
现在检查传递的密钥是否与原始密钥相似,如下所示

if($key==$original_key)
{
//confirm e-mail
}
else
{
//invalid key
}

我认为问题在于,您使用的链接服务器没有准备好-base64编码字符串似乎是生成的url中的页面或目录,并且由于它是随机的,因此在php中需要重写规则和相关逻辑

在htaccess文件中,可能有如下规则:

RewriteEngine On
RewriteBase /testEnvironment/   
RewriteRule ^confirmation/([a-zA-Z0-9_-]+)$ confirmation/index.php?data=$1 [NC,L]
/* this will need to be adjusted to your url - is it simply confirmation.php or other? */
注意数据

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['data'] ) ){

        $data=$_GET['data'];
        $key = "12345";


        $queryString = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $key ), urldecode( base64_decode( $data ) ), MCRYPT_MODE_CBC, md5( md5( $key ) ) ), "\0");


        /* there is no return value from parse_str() so you would need to manually retrieve the variables to find `subscriptions` */
        parse_str( $queryString );


        /* does this show anything ? */
        print_r( $queryString );


        /*
        if(!empty( $stringValue )){
          print_r( $stringValue );
        } else{
          exit("Invalid parameters passed");
        }
        */          
    }
?>


My test environment
-------------------

## .htaccess ##
RewriteEngine On
RewriteBase /
RewriteRule ^stackconfirm/([a-zA-Z0-9_-]+)(/)?$ /test/stackconfirm.php?data=$1 [NC,L]



/* PHP script to generate email and confirmation link*/

function encryptLink( $stringValue ){
    $key = "12345";
    $qryStr = "subscriptions=".$stringValue;
    $query = base64_encode( urlencode( mcrypt_encrypt( MCRYPT_RIJNDAEL_256, md5( $key ), $qryStr, MCRYPT_MODE_CBC, md5( md5( $key ) ) ) ) );

    /* for my test I changed this portion to match url structure on test server */
    $link = '/stackconfirm/'.$query;
    return $link;
}


$link=encryptLink('bobby.dazzler@razzledazzle.com');
echo "<a href='$link'>Click here to confirm membership</a>";



stackconfirm.php
----------------

<?php
    if( $_SERVER['REQUEST_METHOD']=='GET' && isset( $_GET['data'] ) ){

        $data=$_GET['data'];
        $key = "12345";

        $queryString = rtrim( mcrypt_decrypt( MCRYPT_RIJNDAEL_256, md5( $key ), urldecode( base64_decode( $data ) ), MCRYPT_MODE_CBC, md5( md5( $key ) ) ), "\0");

        echo '<pre>', $data, PHP_EOL, print_r( $queryString,true ), '</pre>';
    }
?>


Result
------
subscriptions=bobby.dazzler@razzledazzle.com

我的测试环境
-------------------
##.htaccess##
重新启动发动机
重写基/
重写规则^stackconfirm/([a-zA-Z0-9_-]+)(/)?$/test/stackconfirm.php?数据=$1[NC,L]
/*用于生成电子邮件和确认链接的PHP脚本*/
函数encryptLink($stringValue){
$key=“12345”;
$qryStr=“订阅=”.$stringValue;
$query=base64_encode(urlencode(mcrypt_-encrypt(mcrypt_-RIJNDAEL_-256,md5($key))、$qryStr,mcrypt_-MODE_-CBC,md5(md5($key));
/*对于我的测试,我更改了此部分以匹配测试服务器上的url结构*/
$link='/stackconfirm/'.$query;
返回$link;
}
$link=encryptLink('bobby。dazzler@razzledazzle.com');
回声“;
stackconfirm.php
----------------
结果
------
订阅=博比。dazzler@razzledazzle.com

Well。。显然找不到该页面,请确保URL正确?您使用的是哪个框架?你的文件名是什么?因为从外观上看,您没有使用任何框架,这意味着您必须自己实现URL结构,我似乎对此表示怀疑。服务器(可能是apache)是否设置为查找这样的页面,即:
.htaccess-rewrite rules
?我可能错过了它,但您在哪里定义了
$subscriptions
?您将其设置为
$string=$subscriptions,但我找不到它的定义位置。@Jordy我正在用WordPress创建自定义页面模板,我收到消息“传递的参数无效”,它没有回显$stringValue,即使是成功的消息。。。如果(!empty($stringValue)){echo$stringValue;echo“pass”;}或者{exit(“传递的参数无效”);}但是你现在在确认页面中得到了什么吗?没有。我只得到了一个空白页面,没有任何确认消息。我更改了{u get,因为我在我的表单中使用了{u POST}否-你正在单击电子邮件中的链接,不是吗?因此它是得到的