当我调用我的定制php文件时,它在WordPress中保持404‘状态。我遗漏了什么?

时间:2021-01-01 作者:James

我有一个自定义文件,名为:

/wp-content/plugins/listrak-newsletter-api/listrak-newsletter-api.php
当我尝试在WordPress中调用它时,我会被重定向到404页。但该文件在该位置百分之百存在。所以我很困惑。这也是在PHP 7.4上实现的。

前端的HTML表单与php联系。php只是通过soap与第三方通信。

这是HTML:

<div class="block-title"><span>EMAIL NEWSLETTER</span></div>
<div class="tnp tnp-widget">
  <form action="/wp-content/plugins/listrak-newsletter-api/listrak-newsletter-api.php" method="post">
      <p>Sign up for our free email newsletter</p>
      <div class="tnp-field tnp-field-email"><label>Email</label>
      <input class="email" name="listrak-email" required="" type="email"></div>     
      <div class="tnp-field tnp-field-button"><input class="tnp-submit" value="Subscribe now!" type="submit"></div>
      <input name="action" id="action" value="subscribe" type="hidden" />
      <input name="redirect" id="redirect" value="/email-subscribe-success" type="hidden"/>
  </form>
</div>
这是PHP:

<?php

$host = $_SERVER[\'HTTP_HOST\'];

if (isset($_POST[\'action\'])) {
    
    $email = $_POST[\'listrak-email\']; //obtain email from post, place into $email variable
    $email = filter_var($email, FILTER_SANITIZE_EMAIL); //sanitizing email
        
    if ($host == "www.test1.com" || $host == "test1.com") { //if host is, login and use listid
        $sh_param   = array( //setting username & password array
            \'UserName\' => "",
            \'Password\' => ""

        );
        $authvalues = new SoapVar($sh_param, SOAP_ENC_OBJECT); //encoding username and password array
        $headers[]  = new SoapHeader("http://webservices.listrak.com/v31/", \'WSUser\', $sh_param);
        $soapClient = new SoapClient("https://webservices.listrak.com/v31/IntegrationService.asmx?WSDL", array(
            \'trace\' => 1,
            \'exceptions\' => true,
            \'cache_wsdl\' => WSDL_CACHE_NONE,
            \'soap_version\' => SOAP_1_2
        ));
        
        $soapClient->__setSoapHeaders($headers);
        $params = array( //parameters for soap xml integration with listrak
            \'WSContact\' => array(
                \'EmailAddress\' => $email,
                \'ListID\' => \'\'
            ),
            \'ProfileUpdateType\' => \'Overwrite\',
            \'OverrideUnsubscribe\' => true
        );
        
        try {
            
            $rest = $soapClient->SetContact($params); //using SetContact method, send parameters
            
        }
        catch (SoapFault $e) { //if an error occurs, display it
            
            echo \'<pre>\';
            
            print($e->getMessage());
            
            echo \'</pre>\';
        }
    }
}
$redirect = $_POST[\'redirect\'];
header(\'Location: \' . $redirect); 
?>

1 个回复
SO网友:Q Studio

您不需要将表单提交给PHP脚本,PHP只需要包含或需要脚本,这样就可以将其中的代码提供给整个应用程序。

正如Tom在评论中指出的那样,这种方法存在安全风险,但也不是必需的——大多数PHP应用程序在每次页面加载时都包含数百或数千个文件——这里的诀窍是找不到将代码包含在页面加载流的正确部分的安全且性能良好的方法。

当您想要捕获已发布的表单数据时,需要检查$\\u POST对象中已从表单发布的数据,然后验证它并运行其他健全性检查,然后对其进行处理。