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
Related
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();
I have the following problem: I have a set of domains with the same url structure:
domain-a.com/london/
domain-b/london/
domain-c/london/
I want to do the following thing:
If you are on domain-a.com/london/, I want "related" links underneath pointing to domain-b.com/london/ and domain-c.com/london/
I want these links to appear automatically using the URL of the current page, remove the domain so that only the rest is left - in my example: /london/ and add the other domains in front of this.
I know I have to use echo $_SERVER['REQUEST_URI']; to get the rest of the URL but I don't know how to create a link using this function.
<?php
$url = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
function generateLink($url, $uri){
if(strpos($url,'domain-a.com') !== false){
$link = 'http://domain-b.com' . $uri;
return $link;
}else if(strpos($url,'domain-b.com') !== false){
$link = 'http://domain-c.com' . $uri;
return $link;
}else if(strpos($url,'domain-c.com') !== false){
$link = 'http://domain-a.com' . $uri;
return $link;
}
}
?>
Link
I am developing a site, I was wondering if I could use PHP in a generic way in order to show a div on the home page, but no on any other page. The code I have so far,
<?php
if host == 'http://domain.com/'; echo {
This should only be shown on the homepage http://domain.com but not on domain.com/directory or sub.domain.com/ !
} else {
};
?>
<?php
$match_domains = array('google.com','www.google.com');
if(in_array($_SERVER['SERVER_NAME'], $match_domains) {
echo 'show on my domain!';
}
?>
Using $_SERVER['SERVER_NAME'] we compare it to our desired domain.
We use in_array to search $match_domains for the current domain. If it's in the array, we show our text...Anything else, we ignore.
<?php
$domain = str_replace('www.','',$_SERVER['SERVER_NAME']); // Strip out www.
$match_domains = array('google.com');
if(in_array($domain, $match_domains) {
echo 'show on my domain!';
}
?>
Since you want the home page why don't you check the file name
if ($_SERVER['SCRIPT_NAME'] == "/index.php") {
echo "<div>Content</div>";
}
until today I've been using following function
function menu_active($pagename)
{
$active = basename($_SERVER['PHP_SELF'], ".php");
if ($active === $pagename) echo "id='active' ";
}
And in HTML
<?php menu_active('page_name'); ?>
to detect which page is active to mark it as current page, but when I changed my page display method it doesn't work anymore. So I wonder how to change the function to make it work. I've tried to declare $pagename as filename.inc.php in the script below, but no use.
I'm using this script to display pages
$pages_dir = 'pages';
if (!empty($_GET['p']))
{
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p = $_GET['p'];
if (in_array($p . '.inc.php', $pages))
{
include ($pages_dir . '/' . $p . '.inc.php');
}
else
{
echo 'Error';
}
}
else
{
include ($pages_dir . '/home.inc.php');
}
I appreciate any help, feedback or logic and ideas of how to do this right.
Instead of checking which script is running, check $_SERVER['REQUEST_URI']. This will give you the relative URL as requested by the client.
Am running the following code to gather some data from my page and store it in my database, however, i need to add some extra functionality to it but i don't seem to be able to do it correctly.
The Code:
// Get Referrer and Page
if (isset($_GET["ref"]))
{
// from javascript
$referer = $_GET["ref"];
$page = ((isset($_SERVER['HTTP_REFERER'])) ? (parse_url($_SERVER['HTTP_REFERER'], PHP_URL_PATH)) : (''));
}
else
{
// from php
$referer = ((isset($_SERVER['HTTP_REFERER'])) ? ($_SERVER['HTTP_REFERER']) : (''));
$page = $_SERVER['PHP_SELF']; // with include via php
}
// Cleanup
if (basename($page) == basename(__FILE__)) $page = "" ;
This script is storing $page as "/site/index.php or /site/about.php", for example. I kinda want it to store it as "Index or About" without the whole /site/ and .php part.
Thanks in advance
Use pathinfo(), for example:
<?php
$page = "/site/index.php";
$page_info = pathinfo($page);
$page_name = $page_info['filename'];
echo $page_name; //output: index
?>