Hello,

I am making an online gallery for a friend and have this (partially listed) folder structure.

root/index.php
root/inc/gallery.inc.php
root/inc/setup.inc.php
root/inc/includes.inc.php

Gallery.inc.php has various functions in it (Get image, album, etc), setup.inc.php has config variables (for my friend's ease of access, contains SQL stuff and date formats), here is what includes.inc.php looks like:

<?php
require "setup.inc.php";
require "gallery.inc.php";
?>

index.php has this code at the top:

<?php
include "inc/includes.inc.php";
?>

What I would like to do is use variables and functions FROM settings.inc.php IN gallery.inc.php.

I can access functions from settings.inc.php in gallery.inc.php but not variables...

No there is no "class ______" calls anywhere and therefore I see no problem with my code.

Any and all help is appreciated,

-- Turt2Live

Dani AI

Generated

Quick summary and a reliable fix.

Because your gallery output runs inside a function, variables defined in the global scope (your config file) are not automatically visible inside that function. When PHP includes a file, the included code inherits the scope of the line where the include happens; but named functions get their own local scope and must be given values explicitly. (php.net)

Best practice: pass the configuration values into the function instead of relying on globals. This makes the function easier to test and avoids accidental coupling. Example pattern:

function renderGallery(int $count, string $baseUrl) {
    // $albumName and $username would come from your DB loop
    $albumLink = $baseUrl . '/album.php?name=' . urlencode($albumName) . '&username=' . urlencode($username);
    $safeTitle = htmlspecialchars($imageTitle, ENT_QUOTES, 'UTF-8');
    echo '<a href="' . htmlspecialchars($imgSrc) . '" title="' . $safeTitle . '">...</a>';
}

Passing the base URL in from the caller keeps your config and presentation separated; see the user-defined functions docs for parameter patterns. (php.net)

Alternatives and cautions: defining a constant (e.g., BASE_URL) in your setup file is fine for a truly global, read-only value and avoids global clutter. Using runtime globals or $GLOBALS works, but it’s less maintainable than passing parameters or constants. If you do include config files, prefer require_once/include_once to avoid double-includes and redefinition problems. (php.net)

One more important note for future-proofing: avoid relying on the old mysql_* extension (used in your gallery code) — it was removed in PHP 7. Move to PDO or MySQLi with prepared statements for security and compatibility. (php.net)

Tie‑back: ’s suggestions (global/define) are technically correct; the reason you saw the problem was the function scope you mentioned. Passing the config or using a constant will give the cleanest, safest result.

Recommended Answers

All 10 Replies

Where is settings.inc.php in included?
In index.php OR includes.inc.php?
And what is the order of include coding?

Sorry, I didn't realize that,

settings.inc.php and setup.inc.php are the same file.

Member Avatar for Member #120589

> settings.inc.php and setup.inc.php are the same file.

O man, now I am confused. :(

In my original post I said in the includes.inc.php file there was this line:

require "setup.inc.php";

But I went on to explain a "settings.inc.php".

Both of these files are the same file.

Can you post how your variable is defined in setup.inc.php and how did you used it in gallery.inc.php?

Setup.inc.php:

<?php
/* START USER CONFIG */
$SQL_HOST =			"localhost";
$SQL_USER = 		"*****";
$SQL_PASS = 		"*****";
$SQL_DATABASE =		"*****";

$DATE_FORMAT =		"d/M/Y H:i:s A";
$TIMEZONE = 		"America/Edmonton";

$LOCATION = 		"";

/* END USER CONFIG */
?>

gallery.inc.php:
(This is partial code, the file is a wee bit long, but it is where I am getting issues.)

$q = mysql_query("SELECT * FROM `images` ORDER BY RAND() LIMIT ".$show);
if(mysql_num_rows($q) > 0){
	$ret = $ret . "<div>";
	while($a = mysql_fetch_array($q)){
		$img_src = 				$a['image_location'];
		$img_name = 			$a['image_title'];
		$img_thumb_src = 		create_thumbnail($img_src, "cache/image_temp_".$img_name.".png", true);
		$album_link = 			$LOCATION . "/album.php?name=".$a['album_name']."&username=".$a['username'];
		$album_name = 			$a['album_name'];
		$username = 			$a['username'];
		$ret = $ret . "<div class='imagecontainer'>";
		$ret = $ret . "<a href='".$img_src."' rel='lightbox-ALBUMNAME' title='".$img_name." (".$album_name.", by ".$username.")'><img src='".$img_thumb_src."'></a><br>";
		$ret = $ret . "<b>".$img_name."</b><br>";
		$ret = $ret . "(<a href='".$album_link."'>".$album_name."</a>)<br>";
		$ret = $ret . "By: <a href='".$LOCATION."/user/".$username."'>".$username."</a>";
		$ret = $ret . "</div>";
	}
	$ret = $ret . "</div>
}";

I am trying to use the $LOCATION variable.

Is the code you have posted here for gallery.inc.php is in any function? Or it is outside? If its inside you need to make it global.
Generally i am always defining configuration variables.
e.g. define('SQL_HOST','localhost'); so that i can use it any where inside function.

Yes it's in a function,

so would I change the $LOCATION to define('LOCATION', ''); and call it in gallery.inc.php as $LOCATION or somehow else?

1) either you need to make all config variable global inside function.
i.e.

function fname()
{
    global $LOCATION;
// now you can use $LOCATION
}

2) or you can define in Setup.inc.php:

define('LOCATION', '');
// now you can use LOCATION (without  $ sign) in function file.
commented: Amazing help +3

Alright, thanks for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.