Jump to content

getting variable from file not working


pcw

Recommended Posts

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 = 'pwithers2009@hotmail.co.uk';
?><?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

 

Link to comment
Share on other sites

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']= 'pwithers2009@hotmail.co.uk';
   $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'];
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.