0

我正在尝试从我的自定义构建的联系表单的 process.php 中要求 wp-blog-header.php。但是,如果我这样做,则不再发送消息。我尝试使用固定路径和相对路径。唯一的区别是,使用固定 url,返回成功消息,但消息无论如何都没有到达我。这是 process.php:

    <?php if( isset($_POST) ){

        require('http://www.bastards-design.de/sebastian/wp-blog-header.php');

        //form validation vars
        $formok = true;
        $errors = array();

        //sumbission data
        $ipaddress = $_SERVER['REMOTE_ADDR'];
        $date = date('d.m.Y');
        $time = date('H:i');

        //form data
        $name = $_POST['name']; 
        $email = $_POST['email'];
        $website = $_POST['website'];
        $enquiry = $_POST['enquiry'];
        $message = $_POST['message'];

        $receiver = $_POST['receiver'];
        $sender = "Niemand";// get_option('admin_email');

        if(empty($name)){
            $formok = false;
            $errors[] = "Sie haben keinen Namen angegeben.";
        }

        if(empty($email)){
            $formok = false;
            $errors[] = "Sie haben keine Emailadresse angegeben.";
        //validate email address
        }elseif(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $formok = false;
            $errors[] = "Sie haben keine gültige Emailadresse angegeben.";
        }

        if(empty($message)){
            $formok = false;
            $errors[] = "Das Nachrichtenfeld ist leer.";
        }
        elseif(strlen($message) < 20){
            $formok = false;
            $errors[] = "Ihre Nachricht muss mindestens 20 Zeichen enthalten.";
        }

        if($formok){
            $headers = "From: {$email}" . "\r\n";
            $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

            $emailbody = "<p><strong>Name: </strong> {$name} </p>
                          <p><strong>Website: </strong> {$website} </p>
                          <p><strong>Nachricht: </strong> {$message} </p>
                          <p>Diese Nachricht wurde am {$date} um {$time} über {$sender} gesendet.</p>";

            if($receiver){
                mail($receiver,"Anfrage ".$name,$emailbody,$headers);
            }
            else{
                mail('Sebastian.Starke1@googlemail.com',"Error",$emailbody,$headers);
            }
        }

        //what we need to return back to our form
        $returndata = array(
            'posted_form_data' => array(
                'name' => $name,
                'email' => $email,
                'website' => $website,
                'enquiry' => $enquiry,
                'message' => $message
            ),
            'form_ok' => $formok,
            'errors' => $errors
        );

        //if this is not an ajax request
        if(empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) !== 'xmlhttprequest'){
            //set session variables
            session_start();
            $_SESSION['cf_returndata'] = $returndata;

            //redirect back to form
            header('location: ' . $_SERVER['HTTP_REFERER']);
        }
    }
4

2 回答 2

2

PHP includerequire包含和评估指定路径的文件。

两者都期望服务器上的路径而不是 URL。当您通过http://www.bastards-design.de/sebastian/wp-blog-header.php时,它只会包含脚本的输出,而不是根据需要评估 PHP 代码。

假设您显示的代码来自位于服务器中/var/www/bastards-design/script.php的文件,并且您要包含的文件位于以下位置,/var/www/bastards-design/sebastian/wp-blog-header.php您可以通过以下方式解决问题:

  • 在您的服务器上使用绝对路径 - 例如:。require '/var/www/bastards-design/sebastian/wp-blog-header.php';
  • 使用相对路径 - 例如:。require 'sebastian/wp-blog-header.php';
于 2013-05-07T16:42:46.963 回答
2

顺便说一句,如果你想包含 worpdress 核心(+主题功能),它对用户更好:

require('/wp-load.php');

并不是

require('/wp-blog-header.php');
于 2014-09-19T09:08:01.997 回答