Simple Web Page Navigation 'Previous Next' Php - php

I have 3 (.php) files/pages for a website (in a folder), I have Previous/Next Links displayed on the bottom of each. What php code on the previous/next link, would help me navigate to the next page.
For Example:
Lets say the pages are Page1.php, Page2.php, Page3.php, and I am currently on Page2.php.
If I want to click on the 'Previous' Link, I want Page1.php to be displayed.
If I click on 'Next' then I want Page3.php to be displayed.
I believe this is called 'pagination'?
Previous
Next
I dont know if this is possible. And I hope I have been clear with describing the problem.
Thanks,
babsdoc

Here is the original code #Fred
$images = "jars/"; # Location of small versions
$big = "samp2/"; # Location of big versions (assumed to be a subdir of above)
$cols = 2; # Number of columns to display
if ($handle = opendir($images)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "samp2" && $file != "Thumbs.db") {
$files[] = $file;
}
}
closedir($handle);
}
foreach($files as $file)
{
$pc="Product Code:".substr($file,0,-4);
<a href=\"$images$big$file\" class=\"swap\"><img src=$images$file title=\"title\"
width=\"100\" height=\"100\">
$pc</a></li>";
}

Providing you're going to keep a sane naming convention throughout (ie pageX.php), then the following should suffice.
(might not be the ideal solution for you but gives you an idea. Can be put in a function/changes made, etc)
$strCurrentPage = basename($_SERVER['REQUEST_URI']);
$strPattern = '/\d+/';
preg_match($strPattern, $strCurrentPage, $strGetPageNumber); // extract numerical value
//set two new vars to same as page number, increment one and subtract one
$strNextPageNum = $strGetPageNumber[0];
$strPreviousPageNum = $strGetPageNumber[0];
$strNextPageNum++;
$strPreviousPageNum--;
//set full filename with new numbers
$strNextPage = 'page'.$strNextPageNum.'.php';
$strPreviousPage = 'page'.$strPreviousPageNum.'.php';
//if file is found then show link
//next page
if (file_exists($strNextPage))
{
echo 'Next Page';
}
else
{
echo "No more pages";
}
//previous page
if (file_exists($strPreviousPage))
{
echo 'Previous Page';
}
else
{
echo "No previous pages";
}

Related

Change urls depending on landing page

I have two landing pages (homepage1 and homepage2). If I land on homepage1, the logo link needs to change to homepage1 and keep it as I go to other pages. The same goes when I land on homepage2. I tried -
if (strstr($_SERVER['HTTP_REFERER'], 'homepage1.php') !== false) {
<a href='homepage1.php'><img src='logo.jpg'></a>
}
elseif (strstr($_SERVER['HTTP_REFERER'], 'homepage2.php') !== false ) {
<a href='homepage2.php'><img src='logo.jpg'></a>
}
It works when I go to one page but anymore than one the url and logo are gone. In other words, it doesn't hold on to the url.
I need it to hold on to the url based on what landing page I land on. And it needs to hold on to the url, no matter how many pages I go to.
Is this possible?
As #DragonYen pointed out , you need to use session variable as it can be used to persist state information between page requests.
session_start();
$ref = $_SERVER['HTTP_REFERER'];
$page = explode("/", $ref);
if($page[3] == "homepage1.php") {
$_SESSION['home'] = 1;
}
else if($page[3] == "homepage2.php") {
$_SESSION['home'] = 2;
}
Now you can check for session variable home
if ($_SESSION['home'] == 1) {
<a href='homepage1.php'><img src='logo.jpg'></a>
}
elseif ($_SESSION['home'] == 2) {
<a href='homepage2.php'><img src='logo.jpg'></a>
}
put this when you no longer need the session
unset($_SESSION['home'];
session_destroy();

Find the Page Urls

I have page and I don't show a specific block to a specific urls.
So my urls are www.example.com/products/product1.html, www.example.com/products/product2.html etc..
So what I want to do. I want to find all the urls that starts with www.example.com/products/ and in those urls, exclude the block.
So far my code for one url is:
<?php
$a = "www.example.com/products/product2";
$p = curPageURL(); ?>
<?php
if($a == $p ){
Dont show the block
?>
But I have 100 urls that I don't show the block.
Is there any change to do for all the urls without write 200 lines of code?
Use PHP's strpos
This function will check if the specified string exists in the URL and if it exists, do not show the block.
$findme = 'www.example.com/products/';
$pos = strpos($mystring, $findme);
if ($pos === FALSE) {
// SHOW BLOCK
}
Try with strpos()
$cururl= "http://.".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$pos = strpos($cururl, '/products/');
if ($pos !== FALSE) {
// products found do your stuff
}
I think you can do this with the in_array()-function like this:
$blockedUrlArray[] = "www.example.com/products/product2";
$blockedUrlArray[] = "www.example.com/products/product3";
.
.
.
$searchUrl = curPageURL();
//if the current Url is not in blocked Urls
if(!in_array($searchUrl, $blockedUrlArray){
//do something
}
//if the url is a blocked url
else{
//do something
}

Do not increment on page reload

I've placed a hit counter on my page. It reads a text file, increments the number in the file, and later in the page, I output the incremented value.
$hitsFile = "hits/exps/stats.txt";
$hits = file($hitsFile);
$hits[0]++;
$fp = fopen($hitsFile , "w");
flock($fh, LOCK_EX);
fwrite($fp , $hits[0]);
fclose($fp);
My problem is that if I reload the page, the code will increment the hits. I don't want that. I thought of using session to fix that, but with session, in order the increment the hits again, I need to exit the site and visit again. I don't want that either.
I want it to increment not when I reload the page but when I revisit the page.
For example, let's say I have two-page website, Home and Contact, and on contact page I have a hit counter. I don't want the hit counter to increment if I reload(refresh) the contact page, but if I leave the contact page and visit homepage, and later revisit the contact page, I want it to increment.
In short, I don't want it to increment on page reload. Is there a way to do that?
In each of your pages, you need to write the page name in the session.
Do something like this:
$_SESSION['page'] = 'contact';
On the pages where you need to count hits, you need to check this session key.
For example, if you were on page 'contact', then $_SESSION['page'] == 'contact'.
Now when you go to visit the 'homepage':
$page = $_SESSION['page'];
if($page != 'homepage')
{
//increment your hits counter
$_SESSION['page'] = 'homepage';
}
I suggest this method, is my preferred, create in root these folders: cnt and log... then put inside cnt folder the following files cnt.php and showcnt.php...
cnt.php
<?php
##############################################################################
# Php Counter With Advanced Technology For The Prevention Of Reloading Pages #
# Version: 1.4 - Date: 13.11.2014 - Created By Alessandro Marinuzzi [Alecos] #
##############################################################################
function cnt($file) {
session_start();
global $pagecnt;
$reloaded = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
$thispage = basename($_SERVER['SCRIPT_FILENAME']);
if (!isset($_SESSION['first_go'])) {
$_SESSION['first_go'] = 1;
$first_go = TRUE;
} else {
$first_go = FALSE;
}
if (!isset($_SESSION['thispage'])) {
$_SESSION['thispage'] = $thispage;
}
if ($_SESSION['thispage'] != $thispage) {
$_SESSION['thispage'] = $thispage;
$new_page = TRUE;
} else {
$new_page = FALSE;
}
$pagecnt = rtrim(file_get_contents($file));
if ((!$reloaded) && ($new_page == TRUE) || ($first_go == TRUE)) {
$fd = fopen($file, 'w+');
flock($fd, LOCK_EX);
fwrite($fd, ++$pagecnt);
flock($fd, LOCK_UN);
fclose($fd);
}
}
?>
showcnt.php
<?php
##############################################################################
# Show Counter Results - v.1.4 - 13.11.2014 By Alessandro Marinuzzi [Alecos] #
##############################################################################
function gfxcnt($file) {
global $number;
$number = rtrim(file_get_contents($file));
$lenght = strlen($number);
$gfxcnt = "";
for ($i = 0; $i < $lenght; $i++) {
$gfxcnt .= $number[$i];
}
$gfxind = "<span class=\"counter\"><span class=\"number\">$gfxcnt</span></span>";
echo $gfxind;
}
?>
Well, then edit your index.php or other php page... and put at the beginning this piece of code:
<?php session_start(); include("cnt/cnt.php"); cnt("log/index.txt"); include("cnt/showcnt.php"); ?>
Well, then edit index.php or other php page... and use this piece of code for reading counter file:
<?php gfxcnt("log/index.txt"); ?>
It's all, I hope you'll find my answer useful :) My counter can write/read multiple php pages...
Source: my blog (https://www.alecos.it/new/101/101.php)
Add session_start(); to the top.
Now change your if to this:
if (!isset($_SESSION['lastpage']) || $_SESSION['lastpage'] != $_SERVER['QUERY_STRING') {
$hits[0]++;
}
$_SESSION['lastpage'] = $_SERVER['QUERY_STRING'];
This will basically force someone to move to another page if they want to increment the counter.
Update the hit count only if the current URL is not stored in $_SESSION['url'].
After updating the hit count, store the current URL in $_SESSION['url'].

Creating an if statement for an included menu

I have a menu that is consistent throughout one of my directories. For example,
Main Page
Documents Page
Photos Page
I would expect to be able to include a menu like this into each page, and I'd be done. However, this time it's a bit different, because the links generated by individual pages have to be different even though the destination is the same. Take the link to the galleries.php page as an example:
From the Home page: <a href='galleries.php?id=<?=$id?>'>
From the Documents page: <a href='galleries.php?id=<?=$doc[lid]?>'>
From the Photos page: <a href='galleries.php?id=<?=$photo[lid]?>'>
From the Productss page: <a href='galleries.php??id=<?=$product[lid]?>'>
>
What I'm doing for now is to copy and paste the menu into each file, and changing the URL as needed, but this isn't a very satisfactory solution. How can I build some sort of if statement in the menu itself so the correct link is generated by the page that is including the menu?
$_SERVER['REQUEST_URI'] can give you the page you're currently on, and you can use this to determine where you should link to.
Example:
function displayGalleryId($lid) {
$uri = $_SERVER['REQUEST_URI'];
switch ($uri) {
case '/home.php':
$link = $id;
break;
case '/documents.php':
$link = $doc[$lid];
break;
// Others here...
default:
$link = 'gallery.php';
}
return $link;
}
Example Usage:
<a href='galleries.php?id=<? displayGalleryId($lid); ?> '>
You need to use $_SERVER global array..
you can use $_SERVER['SCRIPT_FILENAME'] or $_SERVER['REQUEST_URI']
it can be like
function getLink($id)
{
$uri = $_SERVER['REQUEST_URI'];
$is_page_home = (strstr($uri, 'home') === true)?true:false;
$is_page_photos= (strstr($uri, 'photos') === true)?true:false;
$is_page_document = (strstr($uri, 'document') === true)?true:false;
if( $is_page_home )
{
$urlid = $id
}
if( $is_page_photos)
{
$urlid = $doc[$id]
}
if( $is_page_document )
{
$urlid = $photo[$id]
}
return $urlId
}
$urlId = getLink($id)
<a href='galleries.php??id=<?=$urlId?>'>
to learn more about server variables http://php.net/manual/en/reserved.variables.server.php

dynamic include methods

Among the following include methods which is the best to practice and why?
$page = $_GET['page'];
Method 1
$pages = array('home', 'blog', 'about');
if( in_array($page, $pages) )
{
include($page.'.php');
{
else
{
die('Nice Try.');
}
Method 2
if($page = 'home'){
include('home.php');
}else if($page = 'blog'){
include('blog.php');
}else if($page = 'about'){
include('about.php');
}
Method 3
if(str_replace("http://", "gth://", $page) == $page){
include_once $page;
}else{
die('Nice Try.');
}
or any other solutions? I dont prefer method 1 and 2 as it always needs to be updated everytime i add a new page.
extending/maintaining the first way is easiest, second way is worse. third way is no way to go, as it relies on user input to require pages... it is going to be a security hole
I believe that the first one is the best of the lot. You can try the second one, but it's for the freshers. And the third one is a BIG NO, because any fresher hacker could hack your "if" condition, & more loopholes will start creeping in.
As for your problem, on adding a new page to the array, every time a new page is created, for the first method, I have one solution:-
Let's say you're putting all the new pages in one folder "abc". Now just write one file code as the following, to read all the files / pages existing in that folder:-
<?php
$page = $_GET['page'];
$pages = array();
/**
* If you are using all the pages existing in the current folder you are in,
* then use the below variable as:-
* $path = ".";
*/
$path = 'abc/'; // Change the Path here, related to this Folder name
$handle = opendir($path);
while (($file = readdir($handle)) !== false) {
$pages[] = $file;
}
closedir($handle);
if( in_array($page, $pages) ) {
include($page.'.php');
}
else {
die('Nice Try.');
}
?>
So you see that the array is getting filled up dynamically, without the need to mention all the pages you create every time. And you are using the first method only. And keep the including pages in one separate folder, which you will need to include every time, in other main pages.
Hope it helps.

Categories