Jump to content

jbreits

Members
  • Posts

    12
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

jbreits's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Thanks for the reply, but what you wrote really has nothing to do with the issue I was describing. And I did read the manual. I was not trying to store or retrieve any classes in my session, nor is my session a class. In the example posted, there is nothing but a few simple functions and variables. What I didn't understand was why the date() function was causing these problems. The only way I could get it to work was to manually close the session before the page ended. And according the manual, the reason you would do this is if you were trying to access classes in the session write handler, which I am not. The only thing I could think of is that the date() function was part of a class (that I didn't know about) and that class was being freed before writing to the session. That would kind of explain the flaky behavior of it working sometimes and sometimes not, because sometimes the memory location might still have some valid data in it where as other times it may not. Either that, or PHP is destroying other things besides classes before it writes the session. Any thoughts? JBreits
  2. I had a strange session problem crop up a couple days ago. I was able to finally nail it down, and I think I may have fixed it, but I'm not sure why it has happened. Basically, my sessions were working great, although I wasn't storing much in them other than user name and user id (which are obviously only set if the login is correct). Well I decided I needed a new session var that would be used on every page whether logged in or not. At the end of each page I put the following: [code]$_SESSION['LastMessage'] = 'no message';[/code] When I did that, REALLY strange things started to happen. When I would load up some pages, they would load half-way, and the reload, again only making it halfway before stopping. Some pages just would come up blank and some would show an error. I couldn't figure the thing out. I started trying different variable names and there were others that worked and others that didn't work. [code]$_SESSION['Last'] = 'no message';  // works $_SESSION['LastM'] = 'no message';  // works $_SESSION['LastMe'] = 'no message';  // works $_SESSION['LastMes'] = 'no message';  // works $_SESSION['LastMessage'] = 'no message';  // works[/code] I was going nuts. I figured that since I had written my own session handling funcitons, that maybe they were the problem. I switched to PHP standard session and it worked fine. So I nailed it down to my functions. Long story short, I eventually nailed it down the the session write handler. In there, I call date('Y-m-d H:i:s') in order to populate the LastAccessed field in my sessions table. Turns out that it was the call to date that was messing the whole thing up. Took that out and it worked fine. So I set about trying to figure out how to get it to work so I could use it. The manual mentioned something about classes being destroyed before sessions so if the session write method cannot access a class. I didnt assume that date() was a class, but just to test I put a session_write_close() at the end of my page rather than just letting the system close it and it WORKED! Is date() really a class? I am wondering if it this fixed for good or if it will crop up again on a different variable name. Not really sure why some var names worked and some did not. Here is very simplified code that can be used to reproduce the error. I was using PHP 5.1.2 on WinXP. Index.php [code]<?php error_log ('page load start'.$VarName); include_once ('session.php'); StartSession(); ?> <html> <head> </head> <body class="Main"> here we are!!!! <br> PHP 5.1.2<br> WinXP </body> </html> <?php   $_SESSION['LastMessage'] = 'no message';   //session_write_close();   error_log ('page load end'); ?>[/code] session.php: [code]<?php function open($save_path, $session_name) { return true; } function close() { return true; } function read($id) {   return true; } function write($id, $sess_data) {   error_log ('sess_data: '.$sess_data);   error_log ('writing '.date('Y-m-d H:i:s'));   error_log ('writing dummy date');   return true; } function destroy($id) { return true; } function gc($maxlifetime) { return true; } function StartSession () {   error_log ('start session'.$VarName);   session_set_save_handler("open", "close", "read", "write", "destroy", "gc");     session_start();       } ?>[/code]
  3. Thank you for your suggestions. As far as templating goes, I'm not really interested in using a templating system, as I have written one myself. Basically, I write my templates and data files locally, then I run them through a custom application that I wrote which outputs all the files based on the templates. This gives me files with as much static html as possible. The stuff that is dynamic (the PHP portions) obviously are still have to be evaluated at the server. I figure using this method is even more efficient than a templating package. It probably uses a little more disk space, but that's minimal because it's text. I am currently using the PHP mysql functions for database access. From what I see on this forum, the mysql functions are probably the fastest for DB access. For caching search results, I wrote my own routines which I think work very well and are quite efficient. You are right on the API's they could be broken up. I wasn't sure if I needed to, but it looks like I should. Currently, everything is procedural but I could switch to using objects. I've heard that objects are not as efficient as procedural code, but maybe it would be worth switching for usability. I have downloaded the benchmark package from the PEAR website. I will see if I can figure out how to use it. I don't really understand what PEAR is at this point. What exactly does PEAR do? I have apache2triad installed on my test box and I know that has PEAR installed on it. Does my hosting company have to have PEAR installed in order for me to use PEAR packages on their server? Thanks, JBreits
  4. Just wondering what everyone else's thoughts are on these efficiency quetsions. I use a shared hosting plan and I'm afraid that when I go live with my site I will end up eating up too many resources on the server. Of course, I don't know that it will be a problem, but I still want my site to be as efficient as possible. 1. I have several places where the same SQL query is being run. For instance, if a person is viewing a post on my site, the query runs with each page load. The query is not overly complex although it does use 3 tables through 2 joins. Since the content in the post is not likely to change very often, I thought maybe it would be a good idea to cache the query results to a file. Then, when any user loads the page, it looks to the cache file for the results. If the data ever changes, I would flush the cache. Is it worth doing caching in this case or would it be quicker to query the DB? I currently use caches for search results, but these queries are usually more complex, involve pagination, and I actually don't want the search results to update while they are viewing them. I'm just trying to figure out what the threshold is for when caching becomes quicker than querying. If so, how would one handle concurrent access to a 'global' cache file? It seems that concurrent access would only be a problem when the cache was being written initially. After that, it is just read-only. 2. I have been building my site trying to separate the presentation from the processing by putting the processing routines in 'api' files and including those api files in my .php files that are being shown to the user. However, one api file is starting get kind of large (~2000 lines). If I have another file include this api file, but the script really only needs 2-3 functions out of the api file, isn't that wasted resources, or will the other functions not be parsed unless they are used? Thanks, JBreits Thought of a couple more. 3. I have seen that is more efficient to break out of PHP mode for large blocks of static HTML rather than echoing the whole thing out. But if one would be breaking out of PHP to output just a few lines, would that be more efficient still, or would it be best to echo them? 4. Generally speaking, how much time is too much time for a page to be generate on the server? I know that the internet connection can be a bottleneck but I'm not really worried about that as there is nothing I can do about it. Thanks, JBreits
  5. Thanks for your responses. It sounds like I would be wise to cache the search results. Not only for efficience on the server, but also to keep the result set consistently paged to the user. Now I just need to decide if I should cache to a file or to a MSSQL table as was mentioned. I wonder which would be easier on the server. Thanks, JBreits
  6. I have looked around and haven't found any answers that have satisfied me. When doing pagination on a result set from a MySQL query, most places I have looked appear to suggest using the LIMIT option in the query. I understand how this works, but it doesn't appear that this would possibly be the fastest (and easiest on the server) way to do it. Most of the examples I looked at also used a second query to find the total number of records returned by the query. Therefore, for each page load, 2 queries are executed. This can easily be cured by performing the record count query on the first page load and then passing it forward. I can see how limit would speed things up if a very simple query were executed, but what if you are doing a full-text search with and ORDER BY on another field (say a date field). Doesn't SQL have to ready the whole result set to order the rows? If so, doesn't that add overhead? Would it be faster to run the complete query once, and cache the results to a file of some sort? Then when loading the next page, the results are read from the file? Perhaps, you could even cache 1 file per page so no-in file processing has to be done. Does this sound like a more efficient method? Secondly, if you use the LIMIT method, couldn't the user end up with inaccurate pagination? For example, I am on page 2 of a dataset ordered by date (desc) and while I am looking at page 2, 5 new records are inserted in to the table. Now, when I go to page 3 and run my new LIMIT query, I would assume that I would have 5 records that used to be on page 2 show up on page 3. I haven't tested this yet, but it only makes sense. Can anyone comment? Thanks, JBreits
  7. Thanks for the response. I will skip the ADO stuff and keep as much stuff out of PHP as possible. I'll see what I can find out about Zend. Thanks! Jbreits
  8. I really thought I would have more people weigh in on this topic. I'm just trying to create my scripts so that use minimal server resources. 1. What are people's thoughts on ADOdb? Is it more efficient than the mysql functions in PHP? 2. Is embedding PHP in html a more efficient way to go? For example, if I have a form that might be re-populating itself based on the $_POST variables, is it more efficient to jump in to PHP mode once and output the entire form with values, or to primarily use HTML mode, but jump in to PHP mode for each value in the form: <input type="text" name="username" size=30 value="<?PHP echo ($_POST[username]); ?>"> Thanks, jbreits
  9. I'm designing a site that uses php and a MySQL database, but I want to make it as efficient as possible so as to not put too much load on my hosting company's server. I have a couple questions about speeding things up. 1. For MySQL connectivity, is it more efficient to use the standard mysql functions within php or to use the ADOdb classes? How about ADOdb lite? Persistent connections are probably not a good idea right (I seem to have found that they will probably hit the max number of open connections to the database)? 2. If I use ADOdb, should I take advantage of caching? 3. It appears that my hosting company has the Zend optimizer installed (via the phpinfo() function). Do I need to do anything to take advantage of this, or will it automatically optimize my files? 4. Is it more efficient to use static HTML in conjunction with php? i.e. which of these 2 is more efficient (when the concept is applied to a larger scale): php only [code] <?php   if (IsUserLoggedIn ())   {     echo ('You are logged in as: '.$_SESSION['UserName']);     echo ('<br>');     echo ('<a href="logout.php">Logout</a>');   }   echo ('<table width="100%">');      echo ('<tr>');   echo ('<td width="50%">');   echo ('User Name: ');   echo ('</td>');   echo ('<td>');   echo ('<input type="text" name="UserName" id="UserName" size="7" maxlength="50">');   echo ('</td>');   echo ('</tr>');      echo ('<tr>');   echo ('<td>');   echo ('Password: ');   echo ('</td>');   echo ('<td>');   echo ('<input type="password" name="Password" id="Password" size="14" maxlength="50">');   echo ('</td>');   echo ('</tr>');      echo ('</table>'); ?>     [/code] or static html with php [code] <?php   if (IsUserLoggedIn ())   {     echo ('You are logged in as: '.$_SESSION['UserName']);     echo ('<br>');     echo ('<a href="logout.php">Logout</a>');   } ?>     <table width="100%">      <tr>   <td width="50%">   User Name:   </td>   <td>   <input type="text" name="UserName" id="UserName" size="7" maxlength="50">   </td>   </tr>      <tr>   <td>   Password:   </td>   <td>   <input type="password" name="Password" id="Password" size="14" maxlength="50">   </td>   </tr>      </table> [/code] Thanks! jbreits
  10. Thanks for the suggestion, but I think I may have figured it out. It appears that the following 3 values define the amount of time that the session file can remain on the server: session.gc_probability session.gc_divisor session.gc_maxlifetime I checked the default values that were set in the ini file, and I got: probability 1 divisor 100 maxlifetime 1440 From the manual, this appears to mean that each time a session loads, there is a 1/100 (1%) chance that garbage collection runs. If GC does run, a session file will be considered garbage and cleaned up if it has not been accessed in 1440 seconds. That all makes sense, but it does not explain why mine were timing out after only a couple minutes. The I noticed this under the eplanation of gc_maxlifetime: [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] Note: If different scripts have different values of session.gc_maxlifetime but share the same place for storing the session data then the script with the minimum value will be cleaning the data. In this case, use this directive together with session.save_path. [/quote] I my session.save_path was set to the default of /tmp. However, this is a shared server and there could be others running session scripts with lower values of gc_maxlifetime. If that was the case, those scripts might be cleaning up my session files as well. At any rate, I change the save_path to /home/myusername/tmp. And low and behold it worked! I was no longer timing out after a couple minutes. To double check that this was the issue, I set the probability and divisor to 1 (100% chance that GC runs) and the maxlifetime to 60 (seconds). And, as I expected, my sessions began to timeout after 1 min of inactivity. So I have figured out the resolution to the problem. I hope this information can help others. As a follow-up question, now that I have my session working with the standard file-storage method, what are the advantages (if any) of using a database (MySQL) and custom handlers to store the session information? Thanks, jbreits
  11. Thanks for the tip, but it didn't help. I am now testing an even simpler session script: [code] <? session_start() $counter = $_SESSION['counter']; $counter++; $_SESSION['counter'] = $counter; print "You have visited this page $counter times during this session<br>"; ?> [/code] Same sort of thing happens. I refresh several times and the number counts as it's supposed to. After letting it sit for a couple minutes, I refresh and the counter has gone back to 1. Is the session timing out or something? I'm new to sessions, so I'm not quite sure what is going on, but it does seem to be an inactivity thing. bst.breittechnologies.com/counter.php if you want to test it out. Anyone have any ideas? Thanks, jbreits
  12. I'm new to sessions, and found some sample login code on this site. It appears to work fine, index.php checks to see if the user is logged in, if not it sends them to login.php. The login form posts login data to itself where it then validates the login, if the login is good, it redirects to index.php. When index.php detects a logged in user, it simply displays the user's name. The problem occurs after I 'log in' succesfully. I refresh my index.php page, everything ok, wait a while and refresh, ok, wait a while and refresh, it takes me to the login.php becuase the variable is either empty or no longer exists. I can't find a pattern in the number of refreshes or the amount of inactive time. functions.php: [code] <?php function secure () {   if (!($_SESSION["member_id"]) || ($_SESSION["member_id"] == "")) {     Header("Location: ./login.php");     exit();   } } function login_check ($forms) {   $error = "";   $username = $forms["username"];   $password = $forms["password"];   if (trim($username) == "") $error .= "<li>Your username is empty.</li>";   if (trim($password) == "") $error .= "<li>Your password is empty.</li>";   /* from here, do your sql query to query the database to search for existing record with correct username and password */   if (trim($error)!="") return $error; } function login ($forms) {   $username = $forms["username"];   $password = $forms["password"];   /* do your sql query again, but now returning the id of member */   $member_id=$username;   return $member_id; } ?> [/code] login.php [code] <?php // login.php session_start(); include ("functions.php"); if ($_POST) {   $error = login_check($_POST);   if (trim($error)=="") {     $_SESSION["member_id"] = login($_POST);     Header('Location: /index.php');     print "all's good";     exit();   } else {     print "Error:$error";   } } ?> <form method="post"> Username : <input type="text" name="username"><br /> Password : <input type="password" name="password"><br /> <input type="submit" value="Login"> </form> [/code] index.php [code] <?php // index.php include("functions.php"); session_start(); secure(); echo ('logged in as: '.$_SESSION['member_id']); ?>   [/code] Does anyone have any ideas on what I'm doing wrong? Thanks, jbreits
×
×
  • 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.