pcw Posted March 6, 2011 Share Posted March 6, 2011 Hi, I have a variable named $siteName stored in the server.php file in the data directory data/server.php Now in the header.php page I want to retrieve the $siteName variable so it can be used in the header information. This is what I have but it isnt working. server.php <?php $siteName = 'My+Test+Site'; ?><?php $adminEmail = '[email protected]'; ?><?php $sendmailLoc = '/usr/sbin/sendmail'; ?><?php $imgdir = 'http://www.tropicsbay.co.uk/images/'; ?><?php $imgdirbase = '/home/tropicsb/public_html/images/'; ?> header.php <?php include("data/server.php"); $siteName = $_GET['siteName']; echo ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>$siteName Admin Panel</title> <link rel="stylesheet" href="admin.css" type="text/css" /> </head> <div id="pagehead"> <div id="navigation"> <img alt="" src="images/leftNav.gif" height="32" width="4" id="leftNav" /> <img alt="" src="images/rightNav.gif" height="32" width="4" id="rightNav" /> <div id="globalLink"> <a href="admin.php?cmd=manage&username=admin&password=$adminpw" id="gl1" class="glink" onmouseover="ehandler(event,menuitem1);">Users</a><a href="admin.php?cmd=editTemplate&username=admin&password=$adminpw" id="gl2" class="glink" onmouseover="ehandler(event,menuitem2);">Templates</a><a href="admin.php?cmd=mysqlBackup&username=admin&password=$adminpw" id="gl3" class="glink" onmouseover="ehandler(event,menuitem3);">Database</a><a href="admin.php?cmd=paymentLog&username=admin&password=$adminpw" id="gl4" class="glink" onmouseover="ehandler(event,menuitem4);">Payment</a><a href="admin.php?cmd=profileFields&username=admin&password=$adminpw" id="gl5" class="glink" onmouseover="ehandler(event,menuitem5);">Setup</a><a href="admin.php?cmd=changeAdminpw&username=admin&password=$adminpw" id="gl6" class="glink" onmouseover="ehandler(event,menuitem6);">Preferences</a><a href="admin.php?cmd=logout&username=admin&password=$adminpw" id="gl7" class="glink" onmouseover="ehandler(event,menuitem6);">Logout</a></div> </div></div> <div id="pagelayout"> <img alt="" src="images/leftCurve.gif" height="6" width="6" id="left" /> <img alt="" src="images/rightCurve.gif" height="6" width="6" id="right" /> <div id="pageName"> <h2>$siteName Admin Panel<h2> </div>'; ?> Any ideas on how to do this, it seems to work ok in one of my other scripts. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/ Share on other sites More sharing options...
silkfire Posted March 6, 2011 Share Posted March 6, 2011 Your variables have to be global so better to put it in the $GLOBALS variable. Also why are you closing/opening PHP tags on each new variable that's plain stupid. server.php: <? $GLOBALS['siteName']= 'My+Test+Site'; $GLOBALS['adminEmail']= '[email protected]'; $GLOBALS['sendmailLoc']= '/usr/sbin/sendmail'; $GLOBALS['imgdir']= 'http://www.tropicsbay.co.uk/images/'; $GLOBALS['imgdirbase']= '/home/tropicsb/public_html/images/'; ?> header.php: <? require_once 'data/server.php'; $siteName = $GLOBALS['siteName']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/#findComment-1183691 Share on other sites More sharing options...
Kalland Posted March 6, 2011 Share Posted March 6, 2011 Hi, Can't you just access it directly without using $_GET Example <?php // server.php $sitename = 'mySite'; // test.php include 'server.php'; echo $sitename; // Outputs: // mySite ?> Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/#findComment-1183698 Share on other sites More sharing options...
Pikachu2000 Posted March 6, 2011 Share Posted March 6, 2011 The variables do not need to be global. One problem is you're attempting to echo a variable from within a single quoted string. Variables aren't interpolated when in a single quoted string, so you either need to: - change the enclosing quotes to double and escape all the other double quotes in that string (probably not very practical in this case) - concatenate the variable into the string - Step out of php to output the markup, and just enter php to echo the variables Another problem is you're overwriting the value of $siteName with this: $siteName = $_GET['siteName']; so remove it. Alternately, you could look into using HEREDOC syntax for this. Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/#findComment-1183708 Share on other sites More sharing options...
DavidAM Posted March 6, 2011 Share Posted March 6, 2011 Are you really putting the Admin Password in a link on every page? You realize that everyone who sees that page will be able to see the password, right? <div id="globalLink"> <a href="admin.php?cmd=manage&username=admin&password=$adminpw" id="gl1" class="glink" onmouseover="ehandler(event,menuitem1);">Users</a> <a href="admin.php?cmd=editTemplate&username=admin&password=$adminpw" id="gl2" class="glink" onmouseover="ehandler(event,menuitem2);">Templates</a> <a href="admin.php?cmd=mysqlBackup&username=admin&password=$adminpw" id="gl3" class="glink" onmouseover="ehandler(event,menuitem3);">Database</a> <a href="admin.php?cmd=paymentLog&username=admin&password=$adminpw" id="gl4" class="glink" onmouseover="ehandler(event,menuitem4);">Payment</a> <a href="admin.php?cmd=profileFields&username=admin&password=$adminpw" id="gl5" class="glink" onmouseover="ehandler(event,menuitem5);">Setup</a> <a href="admin.php?cmd=changeAdminpw&username=admin&password=$adminpw" id="gl6" class="glink" onmouseover="ehandler(event,menuitem6);">Preferences</a> <a href="admin.php?cmd=logout&username=admin&password=$adminpw" id="gl7" class="glink" onmouseover="ehandler(event,menuitem6);">Logout</a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/#findComment-1183741 Share on other sites More sharing options...
redarrow Posted March 6, 2011 Share Posted March 6, 2011 As david said this i fell of my seat lol. Well spotted 10/10 cheared me up mate Are you really putting the Admin Password in a link on every page? You realize that everyone who sees that page will be able to see the password, right? <div id="globalLink"> <a href="admin.php?cmd=manage&username=admin&password=$adminpw" id="gl1" class="glink" onmouseover="ehandler(event,menuitem1);">Users</a> <a href="admin.php?cmd=editTemplate&username=admin&password=$adminpw" id="gl2" class="glink" onmouseover="ehandler(event,menuitem2);">Templates</a> <a href="admin.php?cmd=mysqlBackup&username=admin&password=$adminpw" id="gl3" class="glink" onmouseover="ehandler(event,menuitem3);">Database</a> <a href="admin.php?cmd=paymentLog&username=admin&password=$adminpw" id="gl4" class="glink" onmouseover="ehandler(event,menuitem4);">Payment</a> <a href="admin.php?cmd=profileFields&username=admin&password=$adminpw" id="gl5" class="glink" onmouseover="ehandler(event,menuitem5);">Setup</a> <a href="admin.php?cmd=changeAdminpw&username=admin&password=$adminpw" id="gl6" class="glink" onmouseover="ehandler(event,menuitem6);">Preferences</a> <a href="admin.php?cmd=logout&username=admin&password=$adminpw" id="gl7" class="glink" onmouseover="ehandler(event,menuitem6);">Logout</a> </div> Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/#findComment-1183746 Share on other sites More sharing options...
pcw Posted March 7, 2011 Author Share Posted March 7, 2011 Thank you Pikachu2000 you are a star As for the other guys, thanks for your concern regarding the password, but it was just relevant as part of the session tests I was doing, and it will be remains. Thanks again. POST CLOSED Quote Link to comment https://forums.phpfreaks.com/topic/229796-getting-variable-from-file-not-working/#findComment-1184115 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.