0

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 "";
}
  ?>
3
  • 2
    could you post relevant bits of code with var_dumps? Commented Oct 14, 2009 at 16:32
  • Do you have sample URL's that are being allowed as duplicates? Do a var_dump or print_r on the $_SESSION['pages'] value, and paste the results here for an instance where you say there are duplicates. Commented Oct 14, 2009 at 16:33
  • @Matt: Yeah, haha. Such n00b code o_0. Anyways, @Bift: from the PHP Docs (php.net/manual/en/function.array-unique.php) for array_unique, it states that it's not meant to work with multi-dimensional arrays (which you are trying to unique-ify). You may have to write your function to guarantee uniqueness within the array. Commented Oct 14, 2009 at 17:09

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.