开发者

unique_array not working

I have the following that adds the current URL开发者_开发知识库 to a session then displays the last 10 products associated with that URL.

The problem is that the unique_array function appears to be allowing the duplicate URLs to be added to the session:

<?php 
// Make URL
function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}
//Insert Current URL in SESSION
    $CurrentPage = curPageURL();
    if(strpos($CurrentPage, '/products/'))
  {
  echo "<div class=\"rhm1-large-box\">
    <div class=\"rhm1-right-large-box\"></div>
      <div class=\"rhm1-left-large-box\"></div>
      <div class=\"largebox-heading\"><strong>Recently viewed products</strong></div>
    <div class=\"rhm1-bg-large-box\"></div>
  </div><div class=\"t\"><div class=\"b\"><div class=\"l\"><div class=\"r\"><div class=\"bl\"><div class=\"br\">
    <div id=\"recent\">";
    $_SESSION['pages'][] = $CurrentPage;
    $_SESSION['pages'] = array_unique($_SESSION['pages']);
    if ( Count ( $_SESSION['pages'] ) > 10 )
    Array_Shift ( $_SESSION['pages'] );
  //remove 1st part of address from array
  foreach ($_SESSION['pages'] as &$value) {
  if(strpos($value, '/products/')) {
  echo "<div class=\"recent_prod\">";
  echo "<div class=\"t\"><div class=\"b\"><div class=\"l\"><div class=\"r\"><div class=\"bl\"><div class=\"br\">";
  $url = $value;
  $id = explode('/', $url);
  $ppname = $id[5];
  $sql = "SELECT * FROM products WHERE product_id='$ppname'";
  $result = mysql_query($sql);        
  $myrow = mysql_fetch_array($result);
  $sqlpid = $myrow['product_id'];
  $sqlpname = $myrow['product_name'];
  $sqlpnameurl = str_replace(" ","_",$value);
  $sqlpcatname = $myrow['category_id'];
  $sqlpimage = $myrow['product_thumb_image'];
  echo "<div class=\"prod-titled-a\"><a href=\"" .$sqlpname. "\">" .$sqlpname. "</a></div><br />";
  echo "<a href=\"".$sqlpnameurl. "\"><img src=\"/productimages/thumb/" .$sqlpimage. "\" border=\"0px\" alt=\"" .$sqlpname. "\"/></a><br />";
  $sql1 = "SELECT * FROM product_xref_options WHERE product_id='$sqlpid'";
  $result1 = mysql_query($sql1);        
  $myrow1 = mysql_fetch_array($result1);
  echo "RRP: &pound;<del>" .$myrow1['wasprice']. "</del><br />";
  echo "<span style=\"color:red; font-size:12px;\">OUR PRICE:<b> &pound;" .$myrow1['price']. "</b></span><br />";
  echo "</div></div>
            </div>
    </div></div></div></div>";
  }
   }
echo "<span class=\"lb-half\"></span><!--[If IE]><span class=\"lb-double\"></span><![endif]--></div>
            </div>
    </div></div></div></div><br clear=\"all\" /><br />";
 }
else
{
echo "";
}
  ?>


You should make sure to handle your session variables prior to your echo statement. You don't want the classic "headers already sent" error:

// get pages array first to avoid sending headers
// typecasted to avoid case of empty session var
$pages = (array) $_SESSION['pages'];

// ensure no leading and trailing whitespaces
$pages[] = trim($CurrentPage);
if (count($pages) > 10) array_shift($pages);

// push to session
$_SESSION['pages'] = array_unique($pages);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜