Jump to content

Recommended Posts

I am designing a backend to a website where user profiles play a major role. Rather than have hundreds of users on at once making queries to the database everytime they view their or another member's profile, I thought of storing all of the information in $_SESSION variables when they first log in. These variables could then be updated and refreshed should they edit any of their profile information.

Are there any downsides to doing it this way? This is my first time creating a backend that has to take heavy traffic into account so I am fairly concerned about reducing the strain. Are there any other possible methods, tips or tricks I could use when it comes to this?

Thank you in advance for any help or suggestions you can offer!

Alex T
Link to comment
https://forums.phpfreaks.com/topic/14757-to-_session-or-not-to-_session/
Share on other sites

So you want to store everyone's profile in each person's session? Bad idea  - it will be difficult(impossible?) to update member Xs session after member Y changes thier profile.

This is exactly what a database is good for, so thats what you should use.
The best advice for reducing strain on a database is: Write intelligent SQL, and minimize the number of querys you perform. eg: don't update the users table every time you change a value for a user. Update all a users values at the end of a script, with 1 query.

Don't nest select statements in loops. (if you must, write the query string with a loop, then perform a single query)

Pretty much the really simple obvius stuff =)
if your expecing lots of changes to a users profile, then definately use the database, but if your not expecting much, use cookies, then the strain is on their computer.  you could simply update the script when a user logs out (tell them that any changes will not be saved unless they logout properly (not just close the browser))

Thanks for the help guys. Even the simple tips can help remind you to go about your coding the right way  ;)

I'm hesitant to go the cookies route as a fair number of people don't use/resent them, but what I am really curious about is the caching method. How would this work and how could I go about implementing it? While I'm familiar with what caching is, I have never tried to do it with PHP before and don't really know where to begin with it.
ew, sessions for the loss... best way (easier than sql databases) is simple ini's... i even use ini's (encrypted) for my logins with autologin depending on IP (unless the user disables this feature, then they are logged in depending on IP for a certain interval of time, or untill they click Log Out)
Well, say for instance you create a directory called [b]cache[/b], then you might do something like this:
The browser has requested: [b]profile.php?user=124[/b]
[code]
<?php
//.... Get your variables etc. $_GET...
$user = $_GET['user'];
//The query to check if cache should be updated
$sql = "SELECT last_cached,last_updated FROM users WHERE user_id=$user";
$res = mysql_query($sql);
$row = mysql_fetch_assoc();
if($row['last_cached']<$row['last_updated'])//Cache is older than data
{
  $sql = "";//Get personal information
  $res = mysql_query($sql);
  /*
   Now do all the stuff you normally do for the profile page.
    */
   $f = fopen('cache/user_profile_'.$user.'.txt','w');//Write to cache/user_profile_124.txt
   $res = fwrite($f,$YourCacheInformation);
   fclose($f);
  //Now echo out the content
  echo($YourCacheInformation);
}
else
{
   //Check that the file exists before this!!!
   $cache = file_get_contents('cache/user_profile_'.$user.'.txt');
   echo($cache);
}[/code]
This is pseudo code but shows how I think you could do it.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.