Im creating a website and one of the features is on the side it will show the user the pages they’ve visited since they got to the website, and they can click any of those names and get it to take them back to that page. I have it working, however when they click back to a certain page if they click through them again it writes out the repeat pages. for instance if say we have 10 pages, main01 to main10. The user gets to page main05, and decides he wants to go back to main03, he click main03 on his history list and goes there just fine, decides he wants to keep going and clicks “continue” this brings him to page main04, which is fine. But the history list becomes:
main01
main02
main03
main04
main05
main03
main04
so what I’ve tried to do is create a method that checks to see if the page visited has already been added to the array, if it has been then it should just echo nothing. if it hasnt then it echos the correct link. but whenever I try it now it just displays the last page you visited, and overwrites that every time you change pages. Here is my code:
if($_POST['visited']){
$_SESSION['visitedpages'][$_SESSION['i']] = $_POST['visited'];
$_SESSION['i']++;
echo "<pre>";
print_r($_SESSION['visitedpages']);
echo "</pre>";
}
if($_SESSION['visitedpages']){
$a_length = count($_SESSION['visitedpages']);
for($x = 0; $x < $a_length; $x++){
$name = $_SESSION['visitedpages'][$x];
$exists = checkifexists($name, $a_length);
if(!$exists){
echo "<a href=\"$name.php\">$name</a><br />";
}
else{
echo "";
}
}
}
function checkifexists($name, $a_length){
for($z = 0; $z < $a_length;$z++){
$existingname = $_SESSION['visitedpages'][$z-1];
if($name === $existingname) {
return true;
}
}
return false;
}
how can I get this working correctly? Any help would be appreciated.
EDIT: Actually it looks like I got it working by checking if it exists when its being written to the array, rather than when its being written as a link. However now whenever I click the link to visit the page (for instance main03 from main05) it goes to main03 but the history list doesn’t display, any input on this?
EDIT2: So I changed it, using in_array, per your suggestion, and its displaying properly but its still listing duplicates. here is the code Im using:
if(in_array($_POSTED['visited'],$_SESSION['visitedpages'])){
echo "";
}
else{
$_SESSION['visitedpages'][$_SESSION['i']] = $_POST['visited'];
$_SESSION['i']++;
echo "<pre>";
print_r($_SESSION['visitedpages']);
echo "</pre>";
$arrayinit = true;
}
and now the array looks like this whenever I visit previous pages:
Array
(
[0] => main01
[1] => main02
[2] => main03
[3] => main04
[4] => main03
)
You need to check your
checkifexistsfunction.You’re checking to see if this page has already been mentioned; if it has, then return true. But what you’re doing is going through the whole of
$_SESSION['visitedpages']to see if a page is in there, and of course it is.Try calling it with:
Then you’ll just check elements earlier in the array to see if they’re duplicates.
For what it’s worth, you might be able to do this more efficiently with in_array