Hi All,
I'm experiencing a speed issue on my site which I believe is due to the way I am handling sessions. If I comment-out the sessions section, or if I do not login as a user and delete any session or domain cookies, the pages render extremely quickly. While sessions are registered pages are extremely slow by comparison.
Please could someone give me any pointers as to what I am doing wrong?
Here's the session part of my code.
<?php
ini_set("arg_separator.output", "&amp;"); // PHP writes sessions with an old-fashioned & which is not XHTML friendly
ini_set("session.use_only_cookies", "1"); // Stop session hijacking from followed links with SIDs
session_start();
include("../includes/config_inc.php");
?>
Then, further down the page I use session_is_registered to check to see if the user is logged in and then print the number of members/guests.
<?php
// Print box showing who is logged in
if(session_is_registered('membername')){
if($membername==""){}else{
include("../includes/config_inc.php");
// Get number of visits and number of imps for this member
$query = "SELECT impressions, visits, last_visited FROM members_details WHERE username = '$membername'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$imps = $row["impressions"];
$visits = $row["visits"];
$last_visit = $row['last_visited'];
}
// If last visit is empty, this is their first visit so set visits to 1 and activate
if($last_visit==""){
$last_visited = date("n.j.Y");
$timestamp = time();
$pages_viewed = $imps + 1;
$query = "UPDATE members_details SET
activated = 'yes',
timestamp = '$timestamp',
visits = '1',
impressions = '$pages_viewed',
last_visited = '$last_visited'
WHERE username = '$membername'";
$result = mysql_query($query, $login);
}
// Activate, set last visited date, and add timestamp so we
// can work out how many members are currently online
$last_visited = date("n.j.Y");
$timestamp = time();
$pages_viewed = $imps + 1; // increment hit counter
// If the last visit date is different to today increment visits
if($last_visit == $last_visited){
$query = "UPDATE members_details SET
activated = 'yes',
timestamp = '$timestamp',
impressions = '$pages_viewed',
last_visited = '$last_visited'
WHERE username = '$membername'";
$result = mysql_query($query, $login);
}
else{
$total_visits = $visits + 1;
$query = "UPDATE members_details SET
activated = 'yes',
timestamp = '$timestamp',
impressions = '$pages_viewed',
visits = '$total_visits',
last_visited = '$last_visited'
WHERE username = '$membername'";
$result = mysql_query($query, $login);
}
?>
<div class="box">
<p class="box-head" ><b><a href="members_area.php">Members' area</a></b></p>
<div class="box-body" >
<p class="left">
<?php
echo "You are logged in as <a href=\"edit_profile.php\" title=\"Click here to change your Private Profile\">$membername</a>. ";
include("../includes/useronline_inc.php"); // Show number of members and guests online
?>
</p>
</div>
</div>
<?php
} // membername not ""
}
else{
// Show normal box
?>
<div class="box">
<p class="box-head" ><b><a href="login.php">You are not logged in</a></b></p>
<div class="box-body" >
<p class="left">
<?php
echo "For full FREE access to this site <a href=\"register.php\">register now</a> or <a href=\"login.php\">click here to login</a>. ";
include("../includes/useronline_inc.php"); // Show number of members and guests online
?>
</p>
</div>
</div>
<?php
} // end of showing normal box
?>
Any pointers on what I am doing wrong would be extremely grateful. Many thanks for taking a look! 🙂