0

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.

Keldo
  • 27
  • 1
  • 5
  • You define it in `base.php`, yet don't include it? – Darren Oct 15 '15 at 01:11
  • Among other things, don't put the recipient email in single quotes. Also, please [don't use `mysql_*`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php?rq=1); the `mysql_*` functions are outdated, [deprecated](http://us3.php.net/manual/en/intro.mysql.php), and insecure. Use [`MySQLi`](http://us3.php.net/manual/en/book.mysqli.php) or [`PDO`](http://us3.php.net/manual/en/intro.pdo.php) instead. – elixenide Oct 15 '15 at 01:14
  • you say the variable doesn't get carried forward. Are you getting an undefined variable notice or is the $site_email = to an empty string or to null or ...? –  Oct 15 '15 at 03:27

2 Answers2

1

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()."'";
codesniffer
  • 1,033
  • 9
  • 22
0

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

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();
?>
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • as stated in my original post, this file (base.php) is included in the header and the header is included in every page. I'm going to test it with the return method as codesniffers example did not work. – Keldo Oct 15 '15 at 01:54
  • Make sure `$row['site_email'];` isn't empy. – Pedro Lobito Oct 15 '15 at 02:01
  • I ran a test just calling and echoing the function and the site_email variable contains the site's email address, however, using what is posted above either with or without the return in the mysql statement have failed to send the email. – Keldo Oct 15 '15 at 02:10
  • I've tested the code above and it works properly, you may have a mis-configuration on your server. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Pedro Lobito Oct 15 '15 at 03:05
  • It yields nothing, and I tested again, still not sending the email using either example. – Keldo Oct 15 '15 at 03:24
  • '' This yields the Sites Email address, yet using it within the email method, it does not send. – Keldo Oct 15 '15 at 03:37