Forum Moderators: coopster

Message Too Old, No Replies

php session array

Is it possible to add a value to a session array on each page visit?

         

topherknowles

5:00 pm on Mar 25, 2010 (gmt 0)

10+ Year Member



Hello, I want to attempt something that I'm not sure is possible!

I have the following script set up as an include on a page:

<?php

// checks if page has auto-refreshed, if not checks if logout cookie set, if unavailable last refresh session used
if (!isset($_SERVER["HTTP_REFERER"])) {
$latest = $_SESSION['lastrefresh'];
} else if (isset($_COOKIE['lastlogout'])) {
$latest = $_COOKIE['lastlogout'];
} else {
$latest = $_COOKIE['lastsessionrefresh'];
}

// selects all comments posted since last logout/refresh
$query_n = "SELECT * FROM tblNotes WHERE Date > '$latest'";
$result_n = mysql_query($query_n);

// adds new comment bubble to job brief in schedule if new comments added
while ($notifications = mysql_fetch_array($result_n)) {
if ($notifications > 0 && ($notifications['JobNo']) == ($jobs[0]) && (strpos($_SERVER["HTTP_REFERER"],$jobs[0]) != true)) {
echo '<a href="jobbrief.php?jobno='.$jobs[0].'"><img src="img/comment_bubble.gif" alt="New comment added to brief" /></a>';
}
}

?>


At the moment it checks if the user has just logged in, if the page has auto-refreshed or they have come from elsewhere and changes the timestamp in the query accordingly.

Then it will publish an image against a job based on a number of factors, one of which is that if the user has come from that job page the image will not display.

This works fine, but I want it to work for multiple pages so that if a user visits several job pages, they can all be excluded from the page.

I think this needs to be done via a Session variable that is added to, so at first it will look like:

$_SESSION['job1']

and as the user visits more pages it becomes:

$_SESSION['job1','job2'] etc.]

so that it can be referred to for multiple jobs.

Hope this makes sense to somebody!

Readie

5:42 pm on Mar 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why don't you just add a divider, and do something like

$_SESSION['jobs'] .= '/|#|job1';
$_SESSION['jobs'] .= '/|#|job2';
$_SESSION['jobs'] .= '/|#|job3';
$job_array = explode("/|#|", $_SESSION['jobs']);

And then every value from
$job_array[1];
To
$job_array[(count($job_array) - 1)]

Will contain the name of a job. So a print_r($job_array); will look something like:

array(
[0] =>
[1] => job1
[2] => job2
[3] => job3
)
(Note that $job_array[0] will be an empty string)