Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

session variables or defined constant variables? 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
What do you think would be best to use session variables or constant variables?

The idea is to load default application settings to variables so that themes are properly set/displayed based on these settings.

Both work but wonder which is better tuned for this kind of application. The variables will hold settings such as background color, font properties, script location or directory, image location or directory, language, etc.

I like the fact that defined constant variables are much shorter by not having to type $_SESSION to reference the variable ...

What say you?





--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
it depends whether you want to make the variables user or session dependent or whether they are constant across all users. if the latter then you should not use session variables and the choice becomes whether you use ordinary variables or constants.

personally, i dislike constants as they cannot be easily used in html output (without breaking out of heredoc or quotes). for template based variables i would typically store them inside a class to guarantee that my code cannot inadvertently overwrite them at runtime. This 'static' requirement for such information is one of the good reasons to use constants.
 
Hi

I think you should tell us more about your theme. Are there only predefined themes, or the visitors can define their own ? Any visitor can change/define the theme, or only logged in users ? Will the theme changes/definitions be persistent, or just for the current session ?

Regarding the color and font changes you should implement it by changing the style sheet.

Feherke.
 
For the time being, the themes are static but I am projecting to add the features where the user will be able to define the look and feel of the page.

So, I intend to eventually have the settings stored on a table for any user who has edited these properties.

That being said, it will not be session based but user preference based. The values will be carried across the entire application and their respective scope should be global or must have the ability to define them at any point from any script.

Since I touch the subject of 'scope', I normally just include the config.php script from index.php, does this mean that the variables are now defined for any and all included scripts there after? What about called functions or methods?

I know, I could test this but I figure I ask as I am preparing to run a couple of tests [shadeshappy]


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

I would do it like this :
[ul]
[li]Create the predefined style sheets in separate files, for example light.css and dark.css.[/li]
[li]Create a [tt]form[/tt] where the user can choose a predefined style sheet or customize his own. For example :
HTML:
<form>
Style sheet :
<select name="style">
<option value="light">Light</option>
<option value="dark">Dark</option>
<option value="user">User defined</option>
</select>
Foreground color : <input type="text" name="color">
Background color : <input type="text" name="background-color">
Font size : <input type="text" name="font-size">
</form>
[/li]
[li]Save the chosen style's name together with the user information and if needed the style data in a separate table.[/li]
[li]Store the chosen style's name in the [tt]$_SESSION[/tt]. Also load it together with the user information when logging in.[/li]
[li]Create a script to output style sheet. For example style.php :
PHP:
header('Content-type: text/css');

if ($_SESSION['style']=='user') {
  $style=load_user_style($_SESSION['userid');
echo <<<ENDOFSTYLE
body {
  color: $style['color'];
  background-color: $style['background-color'];
  font-size: $style['font-size'];
}
ENDOFSTYLE;
} else readfile(file_exists($_SESSION['style'].'.css',$_SESSION['style'].'.css':'light.css');
[/li]
[li]Include the style sheet in every page. For example :
HTML:
<link rel="stylesheet" type="text/css" href="style.php">
[/li]
[/ul]

Feherke.
 
Feherke,

That is exactly the way I've done it. Everything happens off the index.php page. I set variables and based on their value I trigger methods or include scripts to render applicable pages.

Right now, I have a config.php file where I load default properties (later to be based on user preference); this file is included at the very top of my index.php script. Everything there after molds around set preferences.

Well, I guess that my fear of inadequate setup was unfounded but none the less, worth asking.

Thank you for taking your time and valuable assistance!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top