Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Declare variable for sitewide use

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Declare variable for sitewide use

Postby dt118 » Wed Jun 05, 2013 8:19 am

On every page, before the rest of the page loads, I need to:
1. test if a variable exists
2. If it does not, store a value in that variable ONE TIME so that when other pages are loaded it does the check and does not have to store it again.

I know how to do this with the <include> at the top of each page, but I can't even get the store part working.

CODE BEGIN - FIRST PAGE (check_settings.php):

<?php

if (isset($varRegionCode)) {
echo "The region code variable is set to " . $varRegionCode . ".";
}
else {
echo "The region code variable is not set.<br />";
$varRegionCode = "SEVA";
echo "The region code variable is now set to " . $varRegionCode . ".";
}
?>

<a href="check_settings_2.php">Next page</a>


CODE END

========================================

CODE BEGIN - SECOND PAGE (check_settings_2.php):

<?php

if (isset($varRegionCode)) {
echo "The region code variable is set to " . $varRegionCode . ".";
}
else {
echo "The region code variable is not set.<br />";
$varRegionCode = "SEVA";
echo "The region code variable is now set to " . $varRegionCode . ".";
}
?>


CODE END

When I go to the second test page I expect the first part of the IF statement to execute (varRegionCode was declared at the end), but it does not. I do not understand why it does not.
dt118
New php-forum User
New php-forum User
 
Posts: 1
Joined: Wed Jun 05, 2013 8:05 am

Re: Declare variable for sitewide use

Postby Strider64 » Wed Jun 05, 2013 9:05 am

This could be an utilities.inc.php file for example
Code: Select all
session_start();

// Check $varRegionCode in session:
$varRegionCode = (isset($_SESSION['varRegionCode'])) ? $_SESSION['varRegionCode'] : NULL;


check_settings.php
Code: Select all
// Need the utilities file:
require('includes/utilities.inc.php');
$_SESSION['varRegionCode'] = 'SEVA';


check_settings_2.php
Code: Select all
// Need the utilities file:
require('includes/utilities.inc.php');
if ($varRegionCode) {
      echo "The region code variable is set to " . $varRegionCode . ".";
} else {
     $_SESSION['varRegionCode'] = 'DMKZ';
}



Maybe do something like the above?
I just wanted to add NEVER put any sensitive data in $_SESSION like passwords for example.
User avatar
Strider64
New php-forum User
New php-forum User
 
Posts: 69
Joined: Sat Mar 23, 2013 8:24 am


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.