PHP, sessions and Internet Explorer

I have a website with a search form. The results page then displays the search results paginated.

I've just been made aware of a problem with this in certain versions of Internet Explorer. If the results are say paginated over 10 pages, you can see the 1st page of results and you may be able to move to the 2nd or 3rd page, but no further. All that is displayed is the 'no results found' message (coded to display if query has zero result) even though there should be another 7 or 8 pages.

Pagination works fine (as far as I'm aware) in all browsers including Internet Explorer 10, but problem arises in earlier versions of Internet Explorer.

I think I've tracked this down to the session not being maintained in IE but have no idea why or how to fix it.

This is the file for the search results
<?php 
session_start();
   
   header("Cache-control: private");
   

$pagetitle="search";
$strtitle=str_replace("-"," ",substr(substr($_SERVER["REQUEST_URI"],12),0,-4));
$strtitle="Properties for Sale Languedoc Roussilon South France ".$strtitle;

$path= "";
 
require_once('/home/e21abar/public_html/includes/conf.php');
require_once('/home/e21abar/public_html/includes/fonctions.php');

$count=0;
$newurl="";


include("includes/meta.php"); 
$lang=$_SESSION['lang'];

$selected='properties';
//for reordering results
$chosen = array();

If($_POST[orderby]){
 $_SESSION['orderby'] = $_POST[orderby];
}
 if(!$_SESSION['orderby']) { 
   If(($_SESSION['prixmaxt']>0)||($_SESSION['prixmint']>0)){
	  $_SESSION['orderby'] = 'price_asc';
	 }else{

		$_SESSION['orderby'] = 'default';
	 }
}
  $orderby=$_SESSION['orderby'];


if($orderby == 'price_asc'){
    $orderby_query = "order by prix asc";
}else if($orderby == 'price_desc'){
	$orderby_query = "order by prix desc";
}else if($orderby == 'newest'){
	$orderby_query = "order by add_date desc";
}else if($orderby == 'default'){
	$orderby_query = "order by reduced_text desc, views desc";
}else { 
  unset($orderby); 
}

// If $orderby was valid set the selected sort option for the form.
if($orderby){

	$chosen[$orderby] = 'selected="selected"';
}


function ShortenText($text,$chars) {
        // Change to the number of characters you want to display
        $text = $text." ";
        $text = substr($text,0,$chars);
        $text = substr($text,0,strrpos($text,' '));
        $text = $text."...";
        return $text;
    }

 

	$sql=	$_SESSION['sql'];

	$pos = strpos($sql, "order by");
  If ($pos>0){
	  $sql=substr($sql, 0, $pos).$orderby_query;
	}else{
	  $sql=$sql.$orderby_query;
	}
	
			$items = 10;
			$page =1;

 			if(isset($_GET['page']) and is_numeric($_GET['page']) and $page = $_GET['page']){
      	$limit = " LIMIT ".(($page-1)*$items).",$items";
 			}else{
        $limit = " LIMIT $items";
 		 }

		 $result=mysql_query($sql);
		 If($result){
		 		 $aux=mysql_num_rows($result);
		 }else{
		    $aux=0;
		 }
		 $query = mysql_query($sql.$limit, $mysql);

		 if($aux>0){
		    include('includes/newpagination.class.php');
				$target = "results.php";
		    $newurl=$_SERVER['REQUEST_URI'];
	    	$newurl=str_replace(0,"",$newurl);
	    	$newurl=str_replace(1,"",$newurl);
	    	$newurl=str_replace(2,"",$newurl);
	    	$newurl=str_replace(3,"",$newurl);
	    	$newurl=str_replace(4,"",$newurl);
	    	$newurl=str_replace(5,"",$newurl);
	    	$newurl=str_replace(6,"",$newurl);
	    	$newurl=str_replace(7,"",$newurl);
	    	$newurl=str_replace(8,"",$newurl);
	    	$newurl=str_replace(9,"",$newurl);
	    	$newurl=str_replace(".php","",$newurl);
	      $newurl=str_replace("/","",$newurl);
										$pagetext = '<div class="pagination" style="width: 300px; text-align:center; margin: 0 auto;"><a href="property-finder.php">Search Again</a></div>';
										$p = new pagination;
										$p->Items($aux);
										$p->limit($items);
										$p->target($target);
										$p->newurl($newurl);
 										$p->currentPage($page);
										$pagetext .= '<br /><span style="color:#990000;">Sort by </span>';
										$pagetext .= '<form method="post" style="display: inline;" name="orderby_form" action="'.$newurl.'.php">';
										foreach ($HTTP_GET_VARS as $key => $value) {
										   If($key!='orderby'){
											   If (is_array($value)){
												   foreach($value as $key2 => $value2){
													   $pagetext .= '<input type="hidden" name="'.$key.'['.$key2.']" value="'.$value2.'" />';
													 }
												 }else{
										        $pagetext .= '<input type="hidden" name="'.$key.'" value="'.$value.'" />'; 
												 }
											}		
    								}

										$pagetext .= '<select style="color:#990000;" name="orderby" onchange="orderby_form.submit();">';
										$pagetext .= '<option value="">---------------</option>';
										$pagetext .= '<option value="price_asc" '.$chosen["price_asc"].'>Price (Low - High)</option>';
										$pagetext .= '<option value="price_desc" '.$chosen["price_desc"].'>Price (High - Low)</option>';
										$pagetext .= '</select>';
										$pagetext .= '</form><br /><br /><!-- correct file -->';
										If($heading1 !=""){
											$pagetext .= '<h1 class="h1alt">';
											$pagetext .= $h1b[$heading1];
											$pagetext .= '</h1>';	
										}										
										
 										while($row = mysql_fetch_assoc($query)){
															 $id = sprintf("%05d",$row[id_num]);
															 $pagetext .= '<div class="listing">';
															 $pagetext .= '<div class="listimg">';
															 // create url friendly link
															 $city = $row[city];
	 														 $typep = $row[prop_type];
    														 $sql2= "SELECT cityname from city where (cityid = $city)" ;
    														 $sql_query2 = mysql_query($sql2);
    														 $cityname = mysql_result($sql_query2,0,"cityname");
    														 $sql3= "SELECT catname_en,catname_fr from categories where (catid = $typep)" ;
    														 $sql_query3 = mysql_query($sql3);
    													    $typetxt = mysql_result($sql_query3,0,"catname_en");
																	If($row[prestige]==1){
																	  If($lang=="fr"){
																		 $typetxt = mysql_result($sql_query3,0,"catname_fr");
																	   $proplink = "http://fr.prestige-luxury-property-south-france.com/Languedoc_Roussillon_".$cityname."_".$typetxt."_".$row[id_num];
																	  }else{
																		$proplink = "http://www.prestige-luxury-property-south-france.com/Languedoc_Roussillon_".$cityname."_".$typetxt."_".$row[id_num];
																	  }
																	}else{
    													       $proplink = "south_france_property_".$cityname."_".$typetxt."_".$row[id_num];
																	}
															 $proplink =str_replace(',', '', $proplink);
															 $proplink =str_replace(' ', '_', $proplink);
															 $proplink =str_replace('(', '', $proplink);
															 $proplink =str_replace(')', '', $proplink);
															 $proplink =str_replace('&', '&amp;', $proplink);
															 $proplink =str_replace('é', 'e', $proplink);
															 $proplink =str_replace('è', 'e', $proplink);

															 $pagetext .= '<a href="'.$proplink.'">';
															 //get photo descriptions
															 $sqld= "SELECT * from photo_descriptions where (ref_no = '$row[id_num]')" ;
                               $sql_queryd = mysql_query($sqld);
															 if (file_exists($path.'image/'.$id.'/image_1.jpg')) {
															   If (mysql_num_rows(mysql_query($sqld))){
															     if(mysql_result(mysql_query($sqld),0,"i1_desc")==""){
									                   $photo_desc = $typetxt.' '.$cityname;
									                 }else{
									                   $photo_desc = mysql_result(mysql_query($sqld),0,"i1_desc");
									                 }
																}else{
																  $photo_desc = $typetxt.' '.$cityname;
																}
                  						   $pagetext .= '<img src="'.$path.'image/'.$id.'/thumbs/image_1.jpg" width="100px" height="75px" border="0" alt="'.$photo_desc.'" title="'.$photo_desc.'" />&nbsp;';
       												 }
															 if (file_exists($path.'image/'.$id.'/image_2.jpg')) {
															   if(mysql_result(mysql_query($sqld),0,"i2_desc")==""){
									                 $photo_desc = $typetxt.' '.$cityname;
									               }else{
									                 $photo_desc = mysql_result(mysql_query($sqld),0,"i2_desc");
									               }
                  							 $pagetext .= '<img src="'.$path.'image/'.$id.'/thumbs/image_2.jpg" width="100px" height="75px" border="0" alt="'.$photo_desc.'" title="'.$photo_desc.'" />&nbsp;';
       												 }
															 $pagetext .= '</a>';
															 $pagetext .= '</div>';
															 $reducedID = $row[reduced_text];
															 $sql4= "SELECT * from reduced_text where (id = $reducedID)" ;
 		   												 $sql_query4 = mysql_query($sql4);
															 //get correct language title
															 switch($lang){
															    case "fr";																	   
																	   If($row[title_fr]!=""){
																		   $reducedtext = mysql_result($sql_query4,0,"reduced_text_fr");
																		   $text = nl2br($row[title_fr]);
																		 }else{
																		   $reducedtext = mysql_result($sql_query4,0,"reduced_text_en");
																		   $text = nl2br($row[title_en]);
																		 }
																	   break;
															    default;
																	   $reducedtext = mysql_result($sql_query4,0,"reduced_text_en");
															       $text = nl2br($row[title_en]);
																     break;
															 }					 												 
															 $text = str_replace("<strong>","",$text);
															 $text = str_replace("</strong>","",$text);
															 $text = str_replace("</div>","",$text);
															 $text = str_replace("<div>","",$text);
															 
															 if (strlen($text)>230){
															 $text = ShortenText($text,230);
															 }
															 $pagetext .= '<p>';
															 $pagetext .= '<a href="'.$proplink.'">';
															 If ($reducedID>0){
    													   $pagetext .= $reducedtext." ".$text;
															 }else{
															   $pagetext .= $text;
															 }
															 $pagetext .='<br /><br />Price: <b>'.displayprix($row[prix],"en").'</b>';
															  $pagetext .='<br /><br />Ref No: '.$row[id_num];
															 $pagetext .= '</a></p>';
															 $pagetext .= '</div>';
															 $pagetext .= '<hr />';

										}
 																																					

										$pagetext .= '<div class="pagination" style="width: 300px; text-align:center; margin: 0 auto;"><a href="property-finder.php">Search Again</a></div>';
 }else{
 			 							$pagetext = '<h3>There are no properties matching your search criteria</h3>';
		                $pagetext .='<b><a href="'.$url1.'" style="font-size: larger;">&lt;&lt; '.$property['returnpage'].'</a></b>';
 }
 
 
 ?>
 
 
<link type="text/css" rel="stylesheet" href="includes/pagstyle.css" media="screen,projection" />
<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-9634867-1']);
  _gaq.push(['_trackPageview']);
  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>
</head>
<? if($showmap==1){ ?>
<body onload="load()" onunload="GUnload()">
<? }else{ ?>
<body>
<? } ?>
<!-- include header -->
<?php include("includes/header.php"); ?>

<div id="outer"> 
 <div id="container">
 

<?php include("includes/left.php"); ?>

  <div id="inner" style="margin-right: 10px;"> 
	

<? 

 if($aux>0) {
  $p->show();
 }



echo $pagetext; 


 if($aux>0) {
  $p->show();
 }



?>


<!--  finish main content here -->		
    <div id="clearfooter"></div>
		
  </div>  <!-- inner -->

 </div> <!-- container -->
 <!-- include footer -->
<?php include("includes/footer.php"); ?>
</div> <!-- outer -->



</body>

</html>

Open in new window

And this is the included pagination class
<?php
class pagination{
/*
Script Name: *Digg Style Paginator Class
Script URI: http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/
Description: Class in PHP that allows to use a pagination like a digg or sabrosus style.
Script Version: 0.4
Author: Victor De la Rocha
Author URI: http://www.mis-algoritmos.com
*/
		/*Default values*/
		var $total_pages = -1;//items
		var $limit = null;
		var $target = ""; 
		var $page = 1;
		var $adjacents = 4;
		var $showCounter = false;
		var $className = "pagination";
		var $parameterName = "page";
		var $urlF = false;//urlFriendly

		/*Buttons next and previous*/
		var $nextT = "Next";
		var $nextI = "&#187;"; //&#9658;
		var $prevT = "Previous";
		var $prevI = "&#171;"; //&#9668;

		/*****/
		var $calculate = false;

		#Total items
		function items($value){$this->total_pages = (int) $value;}
		
		#how many items to show per page
		function limit($value){$this->limit = (int) $value;}
		
		#Page to sent the page value
		function target($value){$this->target = $value;}
		
		function newurl($value){$this->newurl = $value;}
		
		#Current page
		function currentPage($value){$this->page = (int) $value;}
		
		#How many adjacent pages should be shown on each side of the current page?
		function adjacents($value){$this->adjacents = (int) $value;}
		
		#show counter?
		function showCounter($value=""){$this->showCounter=($value===true)?true:false;}

		#to change the class name of the pagination div
		function changeClass($value=""){$this->className=$value;}

		function nextLabel($value){$this->nextT = $value;}
		function nextIcon($value){$this->nextI = $value;}
		function prevLabel($value){$this->prevT = $value;}
		function prevIcon($value){$this->prevI = $value;}

		#to change the class name of the pagination div
		function parameterName($value=""){$this->parameterName=$value;}

		#to change urlFriendly
		function urlFriendly($value="%"){
				if(eregi('^ *$',$value)){
						$this->urlF=false;
						return false;
					}
				$this->urlF=$value;
			}
		
		var $pagination;

		function pagination(){}
		function show(){
				if(!$this->calculate)
					if($this->calculate())
						echo "<div class=\"$this->className\">$this->pagination</div>\n";
			}
		function getOutput(){
				if(!$this->calculate)
					if($this->calculate())
						return "<div class=\"$this->className\">$this->pagination</div>\n";
			}
		function get_pagenum_link($id){
		    $ft=".php";
				if(strpos($this->target,'?')===false)
						if($this->urlF)
								return str_replace($this->urlF,$id,$this->target);
							else
								//return "$this->target?$this->parameterName=$id";
								If ($id==1){
						      return "$this->newurl$ft";
						    }else{
						      return "$this->newurl$id$ft";
						    }
					else
						//return "$this->target&amp;$this->parameterName=$id";
						If ($id==1){
						 return "$this->newurl$ft";
						}else{
						 return "$this->newurl$id$ft";
						}
			}
		
		function calculate(){
				$this->pagination = "";
				$this->calculate == true;
				$error = false;
				if($this->urlF and $this->urlF != '%' and strpos($this->target,$this->urlF)===false){
						//Es necesario especificar el comodin para sustituir
						echo "Especificaste un wildcard para sustituir, pero no existe en el target<br />";
						$error = true;
					}elseif($this->urlF and $this->urlF == '%' and strpos($this->target,$this->urlF)===false){
						echo "Es necesario especificar en el target el comodin % para sustituir el número de página<br />";
						$error = true;
					}

				if($this->total_pages < 0){
						echo "It is necessary to specify the <strong>number of pages</strong> (\$class->items(1000))<br />";
						$error = true;
					}
				if($this->limit == null){
						echo "It is necessary to specify the <strong>limit of items</strong> to show per page (\$class->limit(10))<br />";
						$error = true;
					}
				if($error)return false;
				
				$n = trim($this->nextT.' '.$this->nextI);
				$p = trim($this->prevI.' '.$this->prevT);
				
				/* Setup vars for query. */
				if($this->page) 
					$start = ($this->page - 1) * $this->limit;             //first item to display on this page
				else
					$start = 0;                                //if no page var is given, set start to 0
			
				/* Setup page vars for display. */
				$prev = $this->page - 1;                            //previous page is page - 1
				$next = $this->page + 1;                            //next page is page + 1
				$lastpage = ceil($this->total_pages/$this->limit);        //lastpage is = total pages / items per page, rounded up.
				$lpm1 = $lastpage - 1;                        //last page minus 1
				
				/* 
					Now we apply our rules and draw the pagination object. 
					We're actually saving the code to a variable in case we want to draw it more than once.
				*/

				if($lastpage > 1){
						if($this->page){
								//anterior button
								if($this->page > 1)
										$this->pagination .= "<a href=\"".$this->get_pagenum_link($prev)."\" class=\"prev\">$p</a>";
									else
										$this->pagination .= "<span class=\"disabled\">$p</span>";
							}
						//pages	
						if ($lastpage < 7 + ($this->adjacents * 2)){//not enough pages to bother breaking it up
								for ($counter = 1; $counter <= $lastpage; $counter++){
										if ($counter == $this->page){
												$this->pagination .= "<span class=\"current\">$counter</span>";
											}else{
												//$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
												//$this->pagination .= "<a href=\"".$target.$counter'.php\">".$counter."</a>";
												If ($counter==1){
												  //$nurl=$this->newurl;
													$nurl=$this->newurl.".php";
												}else{
												  //$nurl=$this->newurl.$counter;
													$nurl=$this->newurl.$counter.".php";
												}
												$this->pagination .= "<a href=\"".$nurl."\">".$counter."</a>";
										 }
									}
							}
						elseif($lastpage > 5 + ($this->adjacents * 2)){//enough pages to hide some
								//close to beginning; only hide later pages
								if($this->page < 1 + ($this->adjacents * 2)){
										for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++){
												if ($counter == $this->page)
														$this->pagination .= "<span class=\"current\">$counter</span>";
													else
														$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
											}
										$this->pagination .= "...";
										$this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
										$this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
									}
								//in middle; hide some front and some back
								elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)){
										$this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
										$this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
										$this->pagination .= "...";
										for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
											if ($counter == $this->page)
													$this->pagination .= "<span class=\"current\">$counter</span>";
												else
													$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
										$this->pagination .= "...";
										$this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
										$this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
									}
								//close to end; only hide early pages
								else{
										$this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
										$this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
										$this->pagination .= "...";
										for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
											if ($counter == $this->page)
													$this->pagination .= "<span class=\"current\">$counter</span>";
												else
													$this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
									}
							}
						if($this->page){
								//siguiente button
								if ($this->page < $counter - 1)
										$this->pagination .= "<a href=\"".$this->get_pagenum_link($next)."\" class=\"next\">$n</a>";
									else
										$this->pagination .= "<span class=\"disabled\">$n</span>";
									if($this->showCounter)$this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
							}
					}

				return true;
			}
	}
?>

Open in new window

LVL 1
fionafentonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Sessions rely on cookies.  IE would (like any browser) need to return the cookies for the server to find the session data storage.

Please read this article and be sure that your script is doing it "right."
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

Then go into the page footer and add something like this:

var_dump($_COOKIE);
var_dump($_SESSION);

Open in new window

When you run the script you will be able to see if IE is returning the cookies correctly and you will be able to see what the server is finding in the session data storage.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryCommented:
Why are you using sessions to track the current page?
Do you have a link to the page?
0
Dave BaldwinFixer of ProblemsCommented:
Which "earlier versions of Internet Explorer"?
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

fionafentonAuthor Commented:
Thanks Ray.
It never occured to me that IE 8/9 would be storing different session variables from other browsers. Printing out the session variables pinpointed the problem.
In IE8 and 9 the value for $_SESSION['sql']  (which is the query for the search results) was changing after page 1 of the results. Strangly the new value was a query used elsewhere on the site but not on the search results pages.  (And I have used $sql variable name on other pages.)
Changing

$sql="select etc";
$_SESSION['sql']=$sql;

to

$sqlsearch="select etc";
$_SESSION['sqlsearch']=$sqlsearch;

solved the problem.

Why it was happening will remain a mystery, but the lesson learned is don't re-use variable names on the same site - make them all unique.
0
Ray PaseurCommented:
There may be something else wrong.  It almost sounds like register globals is in play.  Certainly you would not want to use the same index in the session for two different variables, however with proper awareness of variable scope and object property visibility, it should be easy to avoid name collisions.

But that aside, please explain why you graded this a "C" -- what was wrong with my effort to help
0
fionafentonAuthor Commented:
Sorry Ray - it was meant to be A. I've put in a request for it to be changed.
0
Ray PaseurCommented:
Thank you.  In most modern installations, register globals should be off, but that's not always the case.  You can run phpinfo() to verify the status.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.