Get URL of current page using php and ssi - php

So I got a webpage written in HTML. Menu, Header and Footer are included using SSI (server side includes; .shtml). The website is running on an Apache server.
Now I want to build a language changer that changes the page from [server]/de/index to [server]/en/index in case you change the language from German to English.
Therefore I want to include this into the header. I already found out, that using PHP for getting the current URL is the best way to do it and within the index.html file for example the code is working.
But when I include the code into the SSI, it is returning nothing.
Any clues?
PHP-Code:
<?php
$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url;
?>
Index.html:
<body>
<!--#include file="res/Common/header.shtml" -->
<nav>
<div id='cssmenu'>
<!--#include file="res/Common/menu.shtml" -->
</div>
</nav>
<!--#include file="res/Common/footer.shtml" -->
</body>
</html>
header.shtml:
<div align="right"><p style="margin-top:5px; margin-bottom:5px"><font size="2">Language Selector</font></p></div>

Related

How to include and link menu file into every webpage using HTML?

I have researched some answers that talk about php, javascript, iframes etc. but I have tried a couple and none of them work. I am new to HTML coding.. and coding in general!
<link rel="menu" href="menu.html"> does nothing
<!--#include virtual="/menu.html" --> does nothing (presumably because its a comment?)
<iframe src="page.html"></iframe>
or object... both place the menu in a silly little scroll box.
I want to run my menu on my page as if it were a function in C. Where I can just include it, and it be there, or just link it.
Thanks for the help!
Ryan
webpage file: biology.html
menu file: menu.html
<div class="container">
<img src="homeicon.jpg" width="50" alt="Home">
<div class="redhover">
<div class="dropdown">
<button class="dropbtn">GCSEs</button>
<div class="dropdown-content">
Chemistry
Biology
</div>
</div>
<div class="dropdown">
<button class="dropbtn">A-Levels</button>
<div class="dropdown-content">
Chemistry
Biology
</div>
</div>
<div class="dropdown">
<button class="dropbtn">University</button>
<div class="dropdown-content">
Telecommunications
Electronic Engineering
</div>
</div>
<div class="dropdown">
<button class="dropbtn">More</button>
<div class="dropdown-content">
About me
Youtube
</div>
</div>
</div>
</div>
You can use php to include files on other pages. Here is some example code to get you started:
<?php
require_once('menu.php');
?>
You can put this in your HTML page appropriately, however you must make sure that php can be processed on your server and the file containing php code must end in the .php extension.
There are also other methods of including files via php, see here:
http://php.net/manual/en/function.include.php
and
http://php.net/manual/en/function.require.php
Edit - I'm not a big fan of this approach, but it will work on Github pages.
Create a file called nav.js with your menu defined as a js variable, then use javascript to insert it into an empty div created on each page. This way to update your nav you only have to ever edit nav.js Not pretty but it works
nav.js
var navigation = "<nav>";
navigation += "<ul>";
navigation += "<li>Home</li>";
navigation += "<li>About</li>";
navigation += "</ul>";
navigation += "</nav>";
document.getElementById("navigation").innerHTML = navigation;
Other pages
<div id="navigation"></div>
<!--rest of page goes here.-->
<script src="nav.js"></script>
Fiddle: https://jsfiddle.net/ze3hLxx8/1/
There are multiple ways to include a file into another depending on the backend technology you wish / want / need to use.
PHP
The most common way to do it in php is by using the include or require statement inside a php file.
In your specific case your biology.html file must be converted to a biology.php file and then you can add the relative code to include the file:
<?php include('menu.php');?>
This simple statement will add the content in your menu.php file to the current page. This will not work if php is not present on the server and obviously will not work locally without a local development environment
The differences between require and include can be found on the official documentation:
include: http://php.net/manual/en/function.include.php
require: http://php.net/manual/en/function.require.php
SSI
Another method is to use Server Side Includes. To use the SSI it must be supported and enabled on the webserver. To use SSI you need to change the extension from biology.html to biology.shtml and then add the following statement:
<!--#include file="menu.html" -->
More information on server side includes can be found on wikipedia: https://en.wikipedia.org/wiki/Server_Side_Includes

Sidemenu In codeigniter

I just try to bulid control panel using MVC (codeigniter), I just need way to how make a side menu in home page and when I click in the link in menu the page open in the right side, and the menu stay in the left side.
I need coorect way to do this using MVC.
I use this simple way to make Views to make exactly what your asking.
<?php
//This is mainpage.php
$data['page_id'] = $_GET['page_id'];
?>
<!DOCTYPE HTML>
<html>
<head>
<title></title>
</head>
<body>
<div>
<!-- HERE Comes Side Bar -->
<?php $this->load->view('sidebar'); ?>
</div>
<div>
<!-- HERE Comes Main Content -->
<?php $this->load->view('maincontent',$data) ?>
</div>
</body>
</html>
Mainpage.php is a master page who holds code for both, side menu as well as the content page
This code if for side bar sidebar.php in view folder
<div>
<ul>
<li>Dashboard</li>
<li>Settings</li>
<li>Logout</li>
</ul>
</div>
sidebar.php page only holds different links, but all links have GET variable named "page_id" which decides which page is to be displayed in the content page.
Now, in mainpage.php you can notice that maincontent.php is loaded as view with passing $data which has page_id as variable, which is drived from sidebar. this will help to display the content in content side.
This code is for maincontent.php in view folder
<?php
if(file_exists(APPPATH.'views/'.$page_id.'.php')){
$this->load->view($page_id);
}else{
show_404();
}
this has always worked for me in non ajax page displays...so will work for you to.
Thanks...

I Can't Get The Virtual Include To Work, Do I need an extension?

so I have the include saved in a folder called "include" and the file name is menu. When I upload the site, it doesn't show. Do I need an extension? Do I have to do it with php? Help me please. Here's my code. the website is www.martinshaba.zzl.org
</head>
<body>
<div id="container">
<div id="header_line">
<div class="header">
<!--#include virtual="/inlcude/menu.html" -->
<p>MARTIN SHABA</p>
</div>
</div>
The #include tag is nothing to do with PHP, but is an Apache module. That module itself is, however, rarely used these days, and so you may do better to indeed use PHP, though you will have to name the file with a .php extension for that to be parsed.
<div class="header">
<?php
include "/include/menu.html";
?>
<p>MARTIN SHABA</p> <!-- etc.... -->

How pages render in a home page with PHP?

In ASP.NET MVC applications can use dynamic pages as follows:
<!DOCTYPE html>
<html>
<head>
...
</head>
<body>
<div id="menu">
<ul>
<li>Home</li>
<li>Contact</li>
<li>About</li>
</ul>
</div>
<div id="content">
#RenderBody()
</div>
</body>
</html>
with RenderBody () method that renders the content of a page in another.
How I can render the content pages on a main (index.php) page PHP Case 1, with the Codeigniter Framework and Case 2, normally without the framework?
Excuse my English, as it is not my native language!!
Regards,
Mauriciohz
In Normal PHP,
You can use any of include, require , include_once or require_once.
In Codeigniter,
You can load content part alone with use of below code:
$this->load->view("view_file_name");
here view_file_name is your view file which exist inside the applications/view folder.
You have to give without extension.
For ex:
your content file name is "content.php". applications/view/content.php
You can load as:
$this->load->view("content");// without ".php" extension.
You could use the include or require function in php.
Example
include('path');
require('path');
include_once('path');
require_once('path');
The _once would make sure that the file is never included multiple times throughout the script.

Include URL in php

My form create by php script and between teo file:
index.php
header.php
In index.php, i try in
...
<div class="art-header-png"></div>
<div class="art-header-jpeg"></div>
<?php include("header.php"); ?>
</div>
<div class="art-nav">
<div class="l"></div>
<div class="r"></div>
and my header.php
<div class="art-logo">
<h1 id="name-text" class="art-logo-name">Your Title Goes Here</h1>
<div id="slogan-text" class="art-logo-text">Your Slogan Goes Here</div>
</div>
But it didn't show content inside header.php.
How can i make it work?
First thing, either place header.php in the same folder as index.php or specify the correct path relative to index.php when you are using include (same apply to require).
PHP Include
also check to see if your php is configured correctly as well. Save the following line of code in a php file. Run it and see if you get php window:
<?php
phpinfo();
?>

Categories