Page 1 of 1

General PHP Variables Across Mutiple Pages

Posted: Tue Feb 01, 2011 3:21 pm
by specialtyledsdotcom
Hello!

I have one of those questions that generally is best left to the experts who have been developing PHP code over the past few years. There is really no way to get a quick answer myself because without the proper experience (<1 year of experience myself), I would have to spend hours to find an answer out myself.

My question is:

On any given website, i.e. http://www.example.com, a website which has many pages i.e. example.com/page1.php, example/com/page2.php, etc... How long does PHP store new user variables for any given user login?

What I mean is that, when I store a variable $variable on one of the pages as a user for example.com/page1.php, does PHP have a way of holding on to that variable for say, the next user to come in and use it?

My questions relate to $_POST data, $_SESSION data, and $_SERVER and $_GLOBAL variables, specifically.

I am trying to figure out the most efficient way, as in the fastest (but not necessarily the most secure), the most secure (security wise), and what works just IN GENERAL for passing variables between different pages and storing them for later use.

$_SESSION variables tend to time out at a different rate for any given shared web host server configuration (GoDaddy, InMotionHosting, BlueHost, HostGator...etc...)
$_POST variables seem to just work "behind the scenes" for any given "instance" of calling
$_SERVER variables are also temporary?
$_GLOBAL variables I have really no idea about permanent prevalence throughout?

Obviously MySQL is an option here, but are there alternatives?

I have done my best to search this for general use but with little progress.

Any clarification in a general sense for 'any' of these questions are greatly appreciated!!

-Thanks,

Keith

Re: General PHP Variables Across Mutiple Pages

Posted: Tue Feb 01, 2011 3:31 pm
by John Cartwright
You are describing several different storage types, each with their own purpose.

Firstly,
$_SERVER variables are also temporary?
$_GLOBAL variables I have really no idea about permanent prevalence throughout?
These are not for storage at all. Sure you can put stuff in them, but they will only live for the duration of the request. Basically, you should never write to these. Moving on.

$_SESSION - This is a temporary storage system. Keyword - temporary. Store any data that you will need on subsequent requests, but if this data is lost you should not be concerned.

$_POST - This is a transport. It is not a storage system. Sure, you can transport data over each request, but that forces you to make HTTP POST calls on every single request. Not ideal.

Mysql / any database - Permanent Storage System (unless database is using memory as file system -- another topic)- This is where you want to store data that needs to be long lived and query-able. It is much, much more efficient at retrieving data then most other storage systems, although requires more resources.


What your probably after is a combination of session and mysql. You generally want to avoid hitting the database as much as possible, and caching the data for subsequent requests ($_SESSION is a good fit here). I.e., you grab the user information, store it in the session, and subsequent requests hit the session instead of the database to save IO.

Hope that helps.

P.S., I removed the poll. It is not necessary.

Re: General PHP Variables Across Mutiple Pages

Posted: Thu Feb 03, 2011 11:52 pm
by specialtyledsdotcom
Very helpful. See, you just gave me the answers to questions I would have not found out for a long, long time. Thank you very much - and yes, I see now that the poll is irrelevant. I appreciate your well written and structured format of a response. Crystal Clear now!

May I ask of you, or anyone else for that matter, if the concept of storing .txt files in folders created on the fly is more or less efficient than storing in MySQL? I am curious to know which method is faster and more efficient, especially for larger types of storing procedures. The reason I ask, too, is because I notice that MySQL can sometimes be slow(er) in my mind for my site as opposed to other sites, like facebook, who seem to have faster response times.

Cheers,

Keith

Re: General PHP Variables Across Mutiple Pages

Posted: Fri Feb 04, 2011 1:02 am
by Peec
Writing directly to a file decreases performance because it writes to the hard drive, and hard drives is slow because the mechanic arm and mechanical rotating discs inside the HDD.

Using MySQL you can read/write directly to the memory . i.e. it's faster.

Facebook makes use of cache and tons of optimizations, i.e. php to byte code and more, this is probably why your code is slower. Also if your code has many queries it will be slow, without using caching features like memcached / other cache methods.

Re: General PHP Variables Across Mutiple Pages

Posted: Fri Feb 04, 2011 8:55 am
by John Cartwright
Peec wrote:Writing directly to a file decreases performance because it writes to the hard drive, and hard drives is slow because the mechanic arm and mechanical rotating discs inside the HDD.

Using MySQL you can read/write directly to the memory . i.e. it's faster.

Facebook makes use of cache and tons of optimizations, i.e. php to byte code and more, this is probably why your code is slower. Also if your code has many queries it will be slow, without using caching features like memcached / other cache methods.
Unless you specify a MEMORY storage engine, only data + indexes as big as your RAM will be kept in memory. Otherwise, it will still be performing disk IO.

However, the overhead of a database is not big, and unless you have specific performance requirements your trying to accomplish, I would go ahead and use a database (as there are major benefits to a database). This is what we call pre-optimization, and is considered bad practice :D

Re: General PHP Variables Across Mutiple Pages

Posted: Mon Feb 07, 2011 7:28 pm
by specialtyledsdotcom
Thank you both very much for your responses. John, Peec,I can tell that you both know your stuff and I appreciate your comments and wisdom. Both of your answers are very helpful and exactly what I was looking for.

-Keith

Re: General PHP Variables Across Mutiple Pages

Posted: Wed Apr 01, 2015 2:19 pm
by requinix
draymondchm wrote:Hi, just a quick one on this please. If a user is on a shared hosting package, this won't be possible, isn't it?
What won't be possible?