file_get_contents with query string

前端 未结 4 564
粉色の甜心
粉色の甜心 2020-12-06 22:33

i am trying to send email from php i have one php file with all values & other php template file.

(both files are on same server)

i am using file_get_co

相关标签:
4条回答
  • 2020-12-06 23:23
    $url = sprintf("%s://%s",  isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https' : 'http', $_SERVER['SERVER_NAME']);
    $content = file_get_contents($url."/file.php?test=".$test);
    echo $content;
    
    0 讨论(0)
  • 2020-12-06 23:36

    Please note that you had a filename error in $url which wss trying to load emil_form.php.

    A secondary issue seems to be the path you are using for your call to $url. Try making it an full URL in order to properly parse the file, avoiding any templating you may otherwise have to do. The RTT would be slower but you wouldnt have to change any code. This would look like:

       $url = 'http://www.mysite.com/email_form.php';
       $a = "uname";
       if(($Content = file_get_contents($url. "?uname=".$a)) === false) {
           $Content = "";
       }
    
    0 讨论(0)
  • 2020-12-06 23:37

    Here's an alternative solution... Use the relative path or absolute path as suggested by "cballou" to read file.

    But insted of wirting a php file use a simple text file put you message template in it replace <?php $_GET['uname']; ?> with something unique like %uname% (replacement keys). Read the file content into a variable and replace the replacement keys with your variable like so,

       $url = "/usr/local/path/to/email_form.txt";
           $a = "uname";
           if(($Content = file_get_contents($url)) === false) {
               $Content = "";
           }else{
               $content = str_replace('%uname%', $a, $content);
           }
    
    0 讨论(0)
  • 2020-12-06 23:39

    The real problem would be that you try to read a local file with a http query string behind it. The file system don't understand this and looks for a file called "emil_form.php?uname=uname". $_GET / $_POST / etc only works over a http connection.

    Try to put a placeholder like "%%NAME%%" in your template and replace this after reading the template.

    <?php
    $url = "emil_form.php";
    $a   = "uname";
    if(($Content = file_get_contents($url)) === false) {
       $Content = "";
    }
    $Content = str_replace('%%NAME%%', $a, $Content); 
    // sending mail....
    

    Template will look like this:

    Your Name is: %%NAME%%
    
    0 讨论(0)
提交回复
热议问题