Link to home
Start Free TrialLog in
Avatar of Brook Braswell
Brook BraswellFlag for United States of America

asked on

session_start() takes long time to run

I have been using php for about 9 months now and have found a problem in my code...
I run a page for my users to input data and I use ajax to create an iframe to store data so that I can perform calculations without the user seeing a lag between inputs as I recalculate my prices after every change in the data.
if I ever refresh the page, the next input causes the browser to seemingly hang for almost 30 seconds.
if have narrowed it down to the session_start()

I modified the code to display the time difference:

<?php
    echo '<div>BEFORE SESSION START= '.date("H:i:s").'</div>';
    session_start();
    echo '<div>AFTER SESSION START= '.date("H:i:s").'</div>';
    if ($_GET['ITEM'] )
...

This shows me that session_start was lasting anywhere from 24 to 32 seconds to run.

Any help?
Avatar of Hube02
Hube02
Flag of United States of America image

Try the following instead. I believe that you should actually be getting an error if you have display errors on. session_start should be called before anything is output. This would generally cause a "headers already sent" error, I think.

This code will display the number of seconds required.
<?php
  
  $start_time = microtime();
  session_start();
  $end_time = microtime();
  $completion_time = number_format(((substr($end_time,0,9)) + (substr($end_time,-10)) - (substr($start_time,0,9)) - (substr($start_time,-10))),4);
  echo $completion_time;
  
?>

Open in new window

Avatar of Brook Braswell

ASKER

I will put this in...
the problem was occurring before I placed the div tags in.
I only placed them there to see if this was the problem and indeed it was...
I have since modified the above to be

<?php
    session_start();
    if ($_GET['ITEM'] )
...
?>

I still have the problem of the session_start taking a long time to run after the refresh.
Can you please run this script and post the output?  Also, I assume that your session IDs are being carried in the URLs - is that happening?

Thanks and regards
<?php
error_reporting(E_ALL);
phpinfo();
session_start();
var_dump($_SESSION);

Open in new window

the output to this is massive...
and contains much information about my server that I would rather not publish for security purposes...
what specifically would you need to see?
If your servers are configured correctly there is nothing to fear from publishing it - it does not reveal passwords or anything like that.  The important  information is the part about the session handler, and the contents from the end of the page, after phpinfo() output to the end of the var_dump() for the session array.
BTW, if you think phpinfo() is massive, wait till you decide you need to print out all the predefined vars and constants!

;-)

~Ray
Here is the session portion of the phpinfo()

session01.jpg
session02.jpg
OK, nothing goofy in there.  Please run this script (slightly modified from the above) and post the output.  I have one other I will post, too.
<?php
error_reporting(E_ALL);
echo "Hello World\n";
session_start();
var_dump($_SESSION);
var_dump($_COOKIE);

Open in new window

See if this increments the counters as you click the button.  Also, where is the site hosted?

Thanks and regards, ~Ray
<?php // RAY_session_test.php
error_reporting(E_ALL);
 
// START THE SESSION (DO THIS FIRST IN EVERY PHP SCRIPT ON EVERY PAGE)
session_start();
 
// SEE IF THE SUBMIT BUTTON WAS CLICKED
if (isset($_POST['fred']))
{
 
// SEE IF THE CHEESE VARIABLE IS SET IN THE SESSION ARRAY
    if(!isset($_SESSION['cheese']))
    {
 
// IF CHEESE IS NOT SET, SET IT TO ONE
        $_SESSION['cheese'] = 1;

    } else {
 
// IF CHEESE IS SET, ADD ONE TO IT
        $_SESSION['cheese']++;
    }
}
// END OF SCRIPT
?>
<html><head><title>Session Test</title></head>
<body>
Currently, $_SESSION["cheese"] contains: <?php echo $_SESSION['cheese']; ?> <br/>
<form method="post">
<input type="submit" value="click" name="fred">
</form>
</body>
</html>

Open in new window

Ray,

Here is the beginning of the output from the modified script 11:44

Hello World array(12347) {   ["loggedin"]=>   string(3) "yes"   ["WEBSTYLE"]=>   string(5) "hor01"   ["uid"]=>   string(6) "client"   ["pwd"]=>   string(4) "demo"   ["CONN"]=>   int(4)   ["ufn"]=>   string(3) "Bob"   ["uln"]=>   string(5) "Avent"   ["cname"]=>   string(14) "Avent Graphics"   ["clientnbr"]=>   string(19) "80000017-1203710244"   ["clientnumber"]=>   string(19) "80000017-1203710244"   ["PaladinStart"]=>   string(3) "yes"   ["clientcity"]=>   string(10) "Loganville"   ["clientstate"]=>   string(2) "GA"   ["CANQUOTE"]=>   bool(true)   ["CANORDER"]=>   bool(true)   ["CANSTATUS"]=>   bool(true)   ["CANPACKSLIP"]=>   bool(true)   ["CANINVOICE"]=>   bool(true)   ["CANINFO"]=>   bool(true)   ["CANMACHINE"]=>   bool(true)   ["CANCALENDAR"]=>   bool(true)   ["CANCREATE"]=>   bool(true)   ["CANCOPY"]=>   bool(true)   ["CANCONFIRM"]=>   bool(true)   ["CANPRINTQUOTE"]=>   bool(true)   ["CANSEECAD"]=>   bool(true)   ["CANCREATEORDER"]=>   bool(true)   ["CANCOPYORDER"]=>   bool(true)   ["CANCONFIRMORDER"]=>   bool(true)   ["CANPRINTSTATUS"]=>   bool(true)   ["CANPRINTPACK"]=>   bool(true)   ["CANPRINTINVOICE"]=>   bool(true)   ["CANPRINTINFO"]=>   bool(true)   ["CANPRINTSPECS"]=>   bool(true)   ["ClientPressCount"]=>   int(4)   ["press_sel"]=>   string(4) "4238"   ["QuoteMachine"]=>   string(4) "4238"   ["QuoteMachineName"]=>   string(9) "Bobst 162"   ["QuoteTemplate"]=>   string(8) "T0900002"   ["QuoteItem"]=>   string(9) "Bobst Die"   ["OrderMachine"]=>   string(0) ""   ["OrderTemplate"]=>   string(0) ""   ["OrderItem"]=>   string(0) ""   ["LastField"]=>   string(4) "FLD1"   ["TOTALPRICE"]=>   int(0)   ["CalcCount"]=>   int(21)   ["CurQuoteSeq"]=>   string(0) ""   ["CurOrderSeq"]=>   string(0) ""   ["ores01"]=>   array(17) {     [1]=>     array(48) {       [0]=>       float(420)       ["OffLay_ID"]=>       float(420)       [1]=>       string(9) "Bobst Die"       ["Item"]=>       string(9) "Bobst Die"       [2]=>       string(11) "SpecPkgCost"       ["FldName"]=>       string(11) "SpecPkgCost"       [3]=>       int(0)       ["FldType"]=>       int(0)       [4]=>       string(16) "Special Pkg Cost"       ["FldDescrip"]=>       string(16) "Special Pkg Cost"       [5]=>       string(20) "iif({specpkg},100,0)"       ["Formula"]=>       string(20) "iif({specpkg},100,0)"       [6]=>       int(1)       ["FldOrigin"]=>       int(1)       [7]=>       string(10) "Emobssing?"       ["TagL"]=>       string(10) "Emobssing?"       [8]=>       &string(0) ""       ["TagR"]=>       &string(0) ""       [9]=>       string(4) "Deci"       ["DataType"]=>       string(4) "Deci"       [10]=>       string(4) "TEXT"       ["ObjType"]=>       string(4) "TEXT"       [11]=>       int(-500)       ["TopPos"]=>       int(-500)       [12]=>       int(-500)       ["LeftPos"]=>       int(-500)       [13]=>       int(0)       ["Width"]=>       int(0)       [14]=>       int(270)       ["Height"]=>       int(270)       [15]=>       string(7) "DECI_12"       ["ActField"]=>       string(7) "DECI_12"       [16]=>       int(5)       ["Length"]=>       int(5)       [17]=>       int(0)       ["Pos"]=>       int(0)       [18]=>       int(0)       ["OverRide"]=>       int(0)       [19]=>       float(0)       ["Dependancy"]=>       float(0)       [20]=>       string(1) "0"       ["DefaultValue"]=>       string(1) "0"       [21]=>       bool(false)       ["ShowInPrint"]=>       bool(false)       [22]=>       bool(false)       ["ReadOnly"]=>       bool(false)       [23]=>       bool(false)       ["HideFromWeb"]=>       bool(false)     }     [2]=>     array(48) {       [0]=>       float(318)       ["OffLay_ID"]=>       float(318)       [1]=>       string(9) "Bobst Die"       ["Item"]=>       string(9) "Bobst Die"       [2]=>       string(8) "burncost"       ["FldName"]=>       string(8) "burncost"       [3]=>       int(0)       ["FldType"]=>       int(0)       [4]=>       string(9) "Burn Cost"       ["FldDescrip"]=>       string(9) "Burn Cost"       [5]=>       string(16) "{burninches}*.15"       ["Formula"]=>       string(16) "{burninches}*.15"       [6]=>       int(1)       ["FldOrigin"]=>       int(1)       [7]=>       string(17) "Special Packaging"       ["TagL"]=>       string(17) "Special Packaging"       [8]=>       string(0) ""       ["TagR"]=>       string(0) ""       [9]=>       string(4) "Deci"       ["DataType"]=>       string(4) "Deci"       [10]=>       string(4) "TEXT"       ["ObjType"]=>       string(4) "TEXT"       [11]=>       int(-500)       ["TopPos"]=>       int(-500)       [12]=>       int(-500)       ["LeftPos"]=>       int(-500)       [13]=>       int(0)       ["Width"]=>       int(0)       [14]=>       int(270)       ["Height"]=>       int(270)       [15]=>       string(7) "DECI_08"       ["ActField"]=>       string(7) "DECI_08"       [16]=>       int(5)       ["Length"]=>       int(5)       [17]=>       int(0)       ["Pos"]=>       int(0)       [18]=>       int(0)       ["OverRide"]=>       int(0)       [19]=>       float(0)       ["Dependancy"]=>       float(0)       [20]=>       string(1) "0"       ["DefaultValue"]=>       string(1) "0"       [21]=>       bool(false)       ["ShowInPrint"]=>       bool(false)       [22]=>       bool(false)       ["ReadOnly"]=>       bool(false)       [23]=>       bool(false)       ["HideFromWeb"]=>       bool(false)     }     [3]=>     array(48) {       [0]=>       float(319)       ["OffLay_ID"]=>       float(319)       [1]=>       string(9) "Bobst Die"       ["Item"]=>       string(9) "Bobst Die"       [2]=>       string(9) "knifecost"       ["FldName"]=>       string(9) "knifecost"       [3]=>       int(0)       ["FldType"]=>       int(0)       [4]=>       string(10) "Knife Cost"       ["FldDescrip"]=>       string(10) "Knife Cost"       [5]=>       string(17) "{knifeinches}*.35"       ["Formula"]=>       string(17) "{knifeinches}*.35"       [6]=>       int(1)       ["FldOrigin"]=>       int(1)       [7]=>       string(0) ""       ["TagL"]=>       string(0) ""       [8]=>       string(0) ""       ["TagR"]=>       string(0) ""       [9]=>       string(4) "Deci"       ["DataType"]=>       string(4) "Deci"       [10]=>       string(4) "TEXT"       ["ObjType"]=>       string(4) "TEXT"       [11]=>       int(-500)       ["TopPos"]=>       int(-500)       [12]=>       int(-500)       ["LeftPos"]=>       int(-500)       [13]=>       int(0)       ["Width"]=>       int(0)       [14]=>       int(270)       ["Height"]=>       int(270)       [15]=>       string(7) "DECI_09"       ["ActField"]=>       string(7) "DECI_09"       [16]=>       int(5)       ["Length"]=>       int(5)       [17]=>       int(0)       ["Pos"]=>       int(0)       [18]=>       int(0)       ["OverRide"]=>       int(0)       [19]=>       float(0)       ["Dependancy"]=>       float(0)       [20]=>       string(1) "0"       ["DefaultValue"]=>       string(1) "0"       [21]=>       bool(false)       ["ShowInPrint"]=>       bool(false)       [22]=>       bool(false)       ["ReadOnly"]=>       bool(false)       [23]=>       bool(false)       ["HideFromWeb"]=>       bool(false)     }     [4]=>     array(48) {       [0]=>       float(320)       ["OffLay_ID"]=>       float(320)       [1]=>       string(9) "Bobst Die"       ["Item"]=>       string(9) "Bobst Die"       [2]=>       string(8) "woodcost"       ["FldName"]=>       string(8) "woodcost"       [3]=>       int(0)       ["FldType"]=>       int(0)       [4]=>       string(9) "Wood Cost"       ["FldDescrip"]=>       string(9) "Wood Cost"       [5]=>       string(21) "({xsize}*{ysize})*.05"       ["Formula"]=>       string(21) "({xsize}*{ysize})*.05"       [6]=>       int(1)       ["FldOrigin"]=>       int(1)       [7]=>       string(0) ""       ["TagL"]=>       string(0) ""       [8]=>       string(0) ""       ["TagR"]=>       string(0) ""       [9]=>       string(4) "Deci"       ["DataType"]=>       string(4) "Deci"       [10]=>       string(4) "TEXT"       ["ObjType"]=>       string(4) "TEXT"       [11]=>       int(-500)       ["TopPos"]=>       int(-500)       [12]=>       int(-500)       ["LeftPos"]=>       int(-500)       [13]=>       int(0)       ["Width"]=>       int(0)       [14]=>       int(270)       ["Height"]=>       int(270)       [15]=>       string(7) "DECI_10"       ["ActField"]=>       string(7) "DECI_10"       [16]=>       int(5)       ["Length"]=>       int(5)       [17]=>       int(0)       ["Pos"]=>       int(0)       [18]=>       int(0)       ["OverRide"]=>       int(0)       [19]=>       float(0)       ["Dependancy"]=>       float(0)       [20]=>       string(1) "0"       ["DefaultValue"]=>       string(1) "0"       [21]=>       bool(false)       ["ShowInPrint"]=>       bool(false)       [22]=>       bool(false)       ["ReadOnly"]=>       bool(false)       [23]=>       bool(false)       ["HideFromWeb"]=>       bool(false)     }     [5]=>     array(48) {       [0]=>       float(321)       ["OffLay_ID"]=>       float(321)       [1]=>       string(9) "Bobst Die"       ["Item"]=>       string(9) "Bobst Die"       [2]=>       string(9) "punchcost"       ["FldName"]=>       string(9) "punchcost"       [3]=>       int(0)       ["FldType"]=>       int(0)       [4]=>       string(10) "Punch Cost"       ["FldDescrip"]=>       string(10) "Punch Cost"       [5]=>       string(11) "{punches}*5"       ["Formula"]=>       string(11) "{punches}*5"       [6]=>       int(1)       ["FldOrigin"]=>       int(1)       [7]=>       string(0) ""       ["TagL"]=>       string(0) ""       [8]=>       string(0) ""       ["TagR"]=>       string(0) ""       [9]=>       string(4) "Deci"       ["DataType"]=>       string(4) "Deci"       [10]=>       string(4) "TEXT"       ["ObjType"]=>       string(4) "TEXT"       [11]=>       int(-500)       ["TopPos"]=>       int(-500)       [12]=>       int(-500)       ["LeftPos"]=>       int(-500)       [13]=>       int(0)       ["Width"]=>       int(0)       [14]=>       int(270)       ["Height"]=>       int(270)       [15]=>       string(7) "DECI_11"       ["ActField"]=>       string(7) "DECI_11"       [16]=>       int(5)       ["Length"]=>       int(5)       [17]=>       int(0)       ["Pos"]=>       int(0)
this is from using AJAX..
this is a proprietary quoting site

from a quote.php:

  function SetPrice(iseq){
     if (!oIFrame){
        CreateIFrame();
      }

<?php
   // couple dozen lines of code connecting to sqlanywhere
?>

from an ourindex.php:

     var oIFrame = null;
     var oOFrame = null;
     function createIFrame() {
	   
       var oIFrameElement = document.createElement("iframe");
//       oIFrameElement.style.display = "none";
       oIFrameElement.style.top = "30px";
       oIFrameElement.style.width = "900px";
       oIFrameElement.style.height = "600px";
       oIFrameElement.name = "ourweb";
       oIFrameElement.id = "ourweb";
       oIFrameElement.src = "quoteframe.php?ITEM=x";

       document.body.appendChild(oIFrameElement); 

       oIFrame = frames["ourweb"];

     }

from the quoteframe.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Pragma" content="no-cache" />
</head>

<script>
   window.onload = function() {
     parent.formReady();
   }

</script>
<?php
    if ($_GET['ITEM'] )
    {
error_reporting(E_ALL);
echo "Hello World\n";
session_start();
var_dump($_SESSION);
var_dump($_COOKIE);

Open in new window

Wow! 12,347 elements in the session array.  That seems like a LOT, but not something that would take 30 seconds to load.

My guess is that the issue is not with the session.  Have you tried timing this:

quoteframe.php?ITEM=x
How do I time that ?
Well, it's not very scientific, but a first cut would be to open it in its own browser window.  Just to see how long it takes.
sounds fair enough :)
about .015 seconds
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am running this locally.

I am also finding that it "might" be an issue with my database.
I have multiple versions of the database for different clients.
I switched to a different db and now it does not lag in the same way...
There is about a half second wait between each input but not the 30 second lag I had with my demonstration database...
Sounds like you have isolated the problem.  In my experience, when there is a performance problem with a web site the issue is ALWAYS in the data model, and that usually means the data base.  Some of the useful ideas include:

Use EXPLAIN to see what MySQL is doing with your queries
Never store images (or any large blob-type data fields) in the DB
Avoid SELECT * - instead chose the columns you want
Use indexes on any columns that are used in WHERE clauses

If those things do not seem to be enough, some programming might be in order - one of the good tools for speeding things up is to do a down-select into a temporary table.  For example, when you want to do a very complex query and it is about the state of Michigan, first select all the Michigan data into a temporary table ENGINE=MEMORY and use that table for your complex work.

Anyway, it sounds like things are moving in the right direction.  Best of luck with it, ~Ray
Ray,
  Thanks for the help...I do not necessarily consider the problem resolved but do believe it to be somewhere in the Sql Anywhere Database.  That does not really make sense to me but I will take it as it is unless the problem crops up with my current alternate DB.
Personally , I find MySQL does not play as nice for me as Anywhere does.  Besides it is a little beefier for my clients.
I really have not done any image storage as a practice since I developed in Foxpro.
I can not see having a good database without taking advantages of indexing.
but I do occasionally do a select * from certain tables.  I will modify that to see if it speeds up my web design.

Thanks again!!! for all your help.. I will aspire to be as PHP wise as you have become.

BCB
@Brook1966: thanks for the points and for your kind words.  I hope all goes well with the project and my instincts, gained from many years of wrestling with these sorts of performance issues, are that you're in good shape now.  Cheers, ~Ray