I'm trying to code the last 5 page names the user viewed on my site and produce it into a list. I'm currently able to get the current page name, but I don't know how to get the previous pages. This is the code I'm using to get the current page name:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
echo $pageName;
?>
PHP Sessions should get you going in the right direction. For example:
session_start();
if(!isset($_SESSION['pages'])) {
$_SESSION['pages'] = array();
}
if(count($_SESSION['pages']) < 5) {
$_SESSION['pages'] [] = $_SERVER['PHP_SELF'];
} else {
echo "Limit reached";
}
print_r($_SESSION['pages']);
I recommend you use PHP Sessions to accomplish this.
So, save the current page name that you want to the sessions variable like so:
<?php
$pageName = basename($_SERVER['PHP_SELF']);
$_SESSION['pageName'] = $pageName;
?>
And then continue to save these names. #Len_D just beat me to the punch with an answer that uses arrays and is likely what you need.
Related
<?php
session_start();
if(empty($_SESSION['track']))
$_SESSION['history'] = array($_SERVER['PHP_SELF']);
else {
$_SESSION['track'][] = $_SERVER['PHP_SELF'];
if(count($_SESSION['track']) > 5)
array_shift($_SESSION['track']);
}
function display()
{
print_r(array_values($_SESSION['track']));
}
?>
i was able to do it using session but i need to use only cookies and php to track the last 5 pages visited.
any idea guys??
Any comment or answer will appreciate. Thanking in advance.
$url = unserialize($_COOKIE['history']);
array_push($url,your_url);
setcookie('history', serialize($url));
onload of every page first retrieve the value of cookie in url and push the current url in it and add set it to cookie
I am new to php, I need small help
I m creating a page in php named index.php
i want when someone view that page automatically a number or anything would be added to the url end
Like www.abc.com/index.php ---> www.abc.com/index.php?abc or ?132
when ever that index page is refreshed it should get a number or any variable in the end
Try this:
<?php
if (!isset($_GET["123"])) {
header("Location: " . $_SERVER["PHP_SELF"] ."?123");
}
$QS = $_SERVER["QUERY_STRING"];
$URL = "http://www.example.com/index.php";
// Check if Anything already assigned
if( empty($QS) ) {
// Generate Any RANDOM Number Here
$NUM = mt_rand(999, 9999);
// Reload Page and assign number
header("Location: {$URL}?{$NUM}");
}
Place this code in the very top of your page
Give this a go
<?php
$num = '123';
if(!isset($_GET[$num])){
header("Location: /path/to/page?$num");
}
my code is related to breadcrumbs.. that is it should display previous page or from where it is navigated and i achieved it partially , while im refreshing 2-3 times im getting the current page not the previous page.. so pl help me on this
my code lies in session.php as
$add = $_SERVER['PHP_SELF'];
if($_SESSION['pageadd'][1]!= $_SESSION['pageadd'][2])
{ $_SESSION['pageadd'][2]= $_SESSION['pageadd'][1];
}
echo $_SESSION['pageadd'][2];
if(($_SESSION['pageadd'][1]!= $add) )
{ $_SESSION['pageadd'][1]= $_SESSION['pageadd'][0];
$_SESSION['pageadd'][0]=$add;
}
What you want isn't a breadcrumb - it's a history for visited pages! This could be achieved with something like this:
if (!isset($_SESSION['pageadd'])) {
$_SESSION['pageadd'] = array();
}
// add page
$_SESSION['pageadd'][] = $_SERVER['PHP_SELF']
// only save last 5 pages
if (count($_SESSION['pageadd'])) > 5) {
array_shift($_SESSION['pageadd']);
}
Try to use $_SERVER['HTTP_REFERER'] it will return you previous url. However you need to store it in some hidden field or session like you are doing now.
Hope this help :)
So i'm writing this code so that you either get forwarded to a certain page if you're the first one to hit the link, or you are sent back to the original page after being displayed a message if you're not what beginner mistake am i making?
<?php
$count = file_get_contents('counter.txt');
$count = trim($count);
if ($count="0")
{
$count = $count + 1;
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
header("Location: newpage.html");
}
else
{
fclose($fl);
echo "Sorry but the item has already been sold out";
header("Location: oldpage.html");
}
?>
As for the delay, you can accomplish it two different ways. The first is to use PHP header (like you are currently doing), but change it to look like this:
<?php
header("refresh:5;url=oldpage.html");
echo "Sorry but the item has already been sold out";
?>
The other way is to echo out a piece of HTML code, the meta-refresh:
<?php
echo '<meta http-equiv="refresh" content="2;url=oldpage.html">';
echo "Sorry but the item has already been sold out";
?>
In both examples, 5 is the amount of seconds until the refresh. Experiment with each one to see if it will fit your needs.
This might be some sort of syntax that I'm not familiar with, but none of my scripts have ever had the
<? code
I simply use
<?
Also since you did not delay our header tag the user will not see the previously echoed statement above it. It will automatically redirect before the page has time to output fully.
I've hit a dead end. I've been working on this for 3 weeks with no result.
I have 10 php pages (1.php, 2.php, ..., 10.php) and a starting page (start.php).
All I want to do is randomize the 10 pages with no repeat, so when I click "next" in the start.php it should go to one of the 10 pages (let's say for example it goes to 4.php). When I click "next" in 4.php it should redirect to another within the 10 pages except for 4.php.
It should continue this until all the numbers (1.php - 10.php) have been displayed. At this point it should randomize again. When I click "next" in the last number .php displayed, it should randomize the number and go back to the first on the random list.
Here's what I have so far:
start.php:
<?php $vidcount = 1; ?>
<? include ("source.php"); ?>
next page
source.php:
<?php
include ("start.php");
$numbers = range(1, $total_songs);
if(($vidcount == $total_songs)||($vidcount == 1)){
shuffle($numbers);
$vidcount = 1;
}
$nextvid[1] = $numbers[0];
$nextvid[2] = $numbers[1];
$nextvid[3] = $numbers[2];
$nextvid[4] = $numbers[3];
$nextvid[5] = $numbers[4];
$nextvid[6] = $numbers[5];
$nextvid[7] = $numbers[6];
$nextvid[8] = $numbers[7];
$nextvid[9] = $numbers[8];
$nextvid[10] = $numbers[9];
?>
1.php, 2.php, ... 10.php:
<?php
include("source.php");?>
<?php echo $vidcount; ?>
next page
<?php $vidcount++;?>
1.php - 10.php have the same code. I also have a source.php which is supposed to keep track of what number has been displayed and re-shuffle when all the numbers have been displayed.
Please help. I'll greatly appreciate any help I can get.
You don't have to use the above code, I don't mind starting from scratch if you have a different idea as long as the code I get works.
Well firstly why do you have ten files when you could just have one file and ?id=X in the URL? But never mind that.
Your best bet is to use a session variable. Something like this:
<?php
session_start();
if( !isset($_SESSION['sequence']) || !$_SESSION['sequence']) {
$_SESSION['sequence'] = shuffle(range(1,10));
}
$next = array_shift($_SESSION['sequence']);
// now use $next to create your "Next page" link.
?>