简体   繁体   中英

site wide variables in email php

OK so this is what I'm trying to do, Use variables from one file, that is included in the sites header and use them in an email.

base.php

$site = mysql_query("SELECT * FROM settings") or die('Error: '. mysql_error() );
$row = mysql_fetch_array($site);

// set the Site email address
$site_email = $row['site_email'];

confirm.php

include 'header.php';
$header="from: '$site_email'";

or

$to="'$site_email'";

It does not carry the variable forward so of course the email never gets sent or if it does by setting the $to to a hard coded email address, it comes from the server root address, not the site address.

Your assistance will be greatly appreciated.

What you're asking here is akin to globals. They are generally considered undesirable for a variety of reasons. Why not put your code into a function and then call the function?

Basic example (need to verify syntax):

base.php

function GetSiteEmailAddress()
{
   $site = mysql_query("SELECT * FROM settings") or die('Error: '. mysql_error() );
   $row = mysql_fetch_array($site);

   // set the Site email address
   $site_email = $row['site_email'];

   return $site_email;
}

confirm.php

include_once 'base.php';

$header="from: '".GetSiteEmailAddress()."'";

I would create script with all the variables you want to use on several scripts, ie:

globalVars.php

<?php
function site_email(){
$site = mysql_query("SELECT * FROM settings") or die('Error: '. mysql_error() );
$row = mysql_fetch_array($site);
return $row['site_email'];
}
?>

Now you just need to include globalVars.php on every file you need site_email

<?php
include_once("globalVars.php");
echo site_email();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM