Jump to content

Cookies?


smonkcaptain

Recommended Posts

Hey all,

 

http://www.adamrowden.co.uk/photo-searchr.asp

 

 

If you visit this link, you'll see on the left side you have a few photos. If you click a photo from that page and then go back to the link i've provided you'll see that it shows you your 'recently viewed photos'. I'm wanting to do exactly that and need some pointers on how i would!?

 

I'd be very grateful for any help

 

Thanks, Jimmy m

Link to comment
Share on other sites

That site specifically stores a session named AR that stored this data for me after viewing planes 1744, and 1745:

rvp4=&rvp3=&rvp2=&rvp1=1745&rvp0=1744

 

So it simply uses a script that tacks on the ID to the session, and probably limits it I would assume.

Link to comment
Share on other sites

Logically, without me coding the whole thing out.

 

You'd want to check if the session is set, if not you can set it equal to $id&

 

If it is set, you would check how many values are in there, I would explode the array using & as the delimeter, if it were over 5 (or however many max you want), use array pop or something similar to get rid of the last element, or maybe even push if the first element is the latest image they've viewed.

 

If it's set and there's less than your max values, you would just append $id& again.

 

Then on the page you explode again and run through a foreach loop, and echo the img tag with $id.jpg.

Link to comment
Share on other sites

Logically, without me coding the whole thing out.

 

You'd want to check if the session is set, if not you can set it equal to $id&

 

If it is set, you would check how many values are in there, I would explode the array using & as the delimeter, if it were over 5 (or however many max you want), use array pop or something similar to get rid of the last element, or maybe even push if the first element is the latest image they've viewed.

 

If it's set and there's less than your max values, you would just append $id& again.

 

Then on the page you explode again and run through a foreach loop, and echo the img tag with $id.jpg.

 

Thank's mate. I'm a newbie but will give it a go!

 

First of all, this code i've started to write doesnt even show up in page info/cookies/sessions? Also i get an error 'Parse error: syntax error, unexpected ';' in /home/fhlinux009/a/aviation-photographs.net/user/htdocs/photo.php on line 25' having the & sign after $id.

 


if(!isset($_SESSION['recent'])){

$_SESSION['recent'] = $id&;
}

Link to comment
Share on other sites

Logically, without me coding the whole thing out.

 

You'd want to check if the session is set, if not you can set it equal to $id&

 

If it is set, you would check how many values are in there, I would explode the array using & as the delimeter, if it were over 5 (or however many max you want), use array pop or something similar to get rid of the last element, or maybe even push if the first element is the latest image they've viewed.

 

If it's set and there's less than your max values, you would just append $id& again.

 

Then on the page you explode again and run through a foreach loop, and echo the img tag with $id.jpg.

 

Thank's mate. I'm a newbie but will give it a go!

 

First of all, this code i've started to write doesnt even show up in page info/cookies/sessions? Also i get an error 'Parse error: syntax error, unexpected ';' in /home/fhlinux009/a/aviation-photographs.net/user/htdocs/photo.php on line 25' having the & sign after $id.

 


if(!isset($_SESSION['recent'])){

$_SESSION['recent'] = $id&;
}

 

Manage to sort the error... just me being silly.

 

This is what i have so far:

 


<?php 

if(!isset($_SESSION['recent'])){

$_SESSION['recent'] = $id.'&';

} else if (isset($_SESSION['recent'])){

$session = $_SESSION['recent'];
$recent =  explode("&", $session);


}

?>

Link to comment
Share on other sites

Session variables can store arrays.

 

For something like this I would probably use array_unshift() and array_pop() to create a lifo queue.  Something like this would work:

 


function addTolastViewed($id) {
  $maxcount = 10;
  array_unshift($_SESSION['lastViewed'], $id);
  if (count($_SESSION['lastViewed']) > $maxcount)
    array_pop($_SESSION['lastViewed']);

}

 

Now whenever someone views a picture, you simply have to call that function and pass it the picture id, and you have a session variable with up to 10 pictures in it.

Link to comment
Share on other sites

Here's a test program to show you:

 

function addTolastViewed($id) {
  $maxcount = 10;
  $r = array();
  if (is_array($_SESSION['lastViewed']))
        $r = $_SESSION['lastViewed'];

  array_unshift($r, $id);
  if (count($r) > $maxcount)
    array_pop($r);
  $_SESSION['lastViewed'] = $r;

}
session_start();
$avalue = rand(1, 1000);
addTolastViewed($avalue);
echo "This id: $avalue ";
echo '';
var_dump($_SESSION['lastViewed']);
echo '';

Link to comment
Share on other sites

Here's a test program to show you:

 

<?php
function addTolastViewed($id) {
  $maxcount = 10;
  $r = array();
  if (is_array($_SESSION['lastViewed']))
        $r = $_SESSION['lastViewed'];

  array_unshift($r, $id);
  if (count($r) > $maxcount)
    array_pop($r);
  $_SESSION['lastViewed'] = $r;

}
session_start();
$avalue = rand(1, 1000);
addTolastViewed($avalue);
echo "This id: $avalue </br>";
echo '<pre>';
var_dump($_SESSION['lastViewed']);
echo '</pre>';

 

That's brill thanks!

 

However, i'm using the function() on one page, and echoing the array on another to dsiplay the recent viewed photos, and getting this error:

 

Fatal error: Call to undefined function addtolastviewed() in /home/fhlinux009/a/aviation-photographs.net/user/htdocs/results.php on line 241

Link to comment
Share on other sites

Ok, ignore my previous post, I've sorted it now.

 

Here is the code i have:

 

<?php 


function addTolastViewed($id) {
if(isset($_GET['id'])){
  $maxcount = 6;
  $lastv = array();
  if (is_array($_SESSION['lastViewed']))
        $lastv = $_SESSION['lastViewed'];

  array_unshift($lastv, $id);
  if (count($lastv) > $maxcount)
    array_pop($lastv);
  $_SESSION['lastViewed'] = $lastv;
}
}


session_start();
addTolastViewed($id);
echo "This id: $id </br>";
echo '<pre>';
var_dump($_SESSION['lastViewed']);
echo '</pre>';


?>

 

 

How can i now display the images using the variables as the id from the array?

 

Thanks all!

Link to comment
Share on other sites

You need the function to be available in the script where you call it.  So if you have it in a functions.php file you'd need to include or require it, or just have it defined somewhere in the script.

 

If you copied and pasted my code into your own, your error could reflect the fact that php is case sensitive. 

 

addtolastviewed()

 

Would need to be

 

addTolastViewed($id)

 

Notice the "camel" casing I used.

Link to comment
Share on other sites

I don't know what these id#'s refer to, but in general , you can foreach through an array()

 


foreach($_SESSION['lastViewed'] as $id) {

// use $id to lookup image info and display it?
}

 

Works perfectly, thankyou very much!

 

One last thing, If i click on an image twice, then the image $id get's posted into the session array twice, how can i stop this from happening and as a result stop duplicate copies of 'recently viewed photos'?

 

Thanks

Link to comment
Share on other sites

That's a good point.  Add some code to the addToLastViewed() function that will strip out prior occurrences of the same id in the queue.  This can be easily done using array_diff.

 

function addTolastViewed($id) {
  $maxcount = 10;
  $r = array();
  if (is_array($_SESSION['lastViewed']))
        $r = $_SESSION['lastViewed'];
  // Check if we've recently viewed this same picture
  if (count($r) > 0) {
        $r = array_diff($r, array($id));
  }
  array_unshift($r, $id);
  if (count($r) > $maxcount)
    array_pop($r);
  $_SESSION['lastViewed'] = $r;
}

Link to comment
Share on other sites

That's a good point.  Add some code to the addToLastViewed() function that will strip out prior occurrences of the same id in the queue.  This can be easily done using array_diff.

 

function addTolastViewed($id) {
  $maxcount = 10;
  $r = array();
  if (is_array($_SESSION['lastViewed']))
        $r = $_SESSION['lastViewed'];
  // Check if we've recently viewed this same picture
  if (count($r) > 0) {
        $r = array_diff($r, array($id));
  }
  array_unshift($r, $id);
  if (count($r) > $maxcount)
    array_pop($r);
  $_SESSION['lastViewed'] = $r;
}

 

That's fantastic! Thanks for all your help guys, much appriciated.  :D

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.