English 中文(简体)
php - transfer $_SESSION var s to local var s?
原标题:

What is best practice with regards to using session variables?

Is it best to just refer to them as session variables or is it better at the beginning of the script to transfer them to local variables of the same name?

I am also a little stumpped on the best folder/file structure for my application if anyone has a useful link with regards to that it would be very useful.. thanks.

最佳回答

Just access them as they are, there will be no performance hit.

In my mind data is usually in session for a reason, so moving it from the session to local, and the having to put it back again just provides a step for errors to occur, plus it may make your code more confusing to read.

You probably only want to assign the session value to a local variable if you need to manipulate the data and want to retain the original value.

问题回答

I usually transfer them to local variables if I don t intend to manipulate them, just to avoid the chance of unintentionally overwriting. Plus, it s easier to work with local variables than writing out $_SESSION[ ] every time.

Is it best to just refer to them as session variables or is it better at the beginning of the script to transfer them to local variables of the same name?

For me it depends on what you are doing with it, if you are using it once then use $_Session[] if you are doing lots of logic with it, it makes sense to transfer it to a local var.

Either way its preferance.

I d recommend against using $_SESSION. Use a Session wrapper/manager class for handling session variables.
There are many available out there, but Zend_Session is among the best.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签