Multiple HTML Pages in one file - php

Hi guys I am making a website for a school project, but to keep in organised, I want to have all pages in one document, and when you go to the site the default page is index.php, and the other pages are linked like this: index.php?p=page
the problem now is, that I had this script, but don't know where I have it. I'm not skilled in PHP too, I got it from a friend of mine. It was like this:
?>
<php? some php code ?>
<html tag etc>
<head> Head stuff </head>
<Nav> <Header>
<php? some php code where I filled in what the link was of the content under it ?>
< The content of the page, excluding the header, navigator etc because that was above here. >
?>
Then I could do the same thing over and over again, and just change the PHP code and it worked.
<footer stuff>
</body>
</html>
I know that there are many scripts for this, and I searched Google for over an hour, but with no succes.
-Tristan

A very basic example of how it works :
<?php
$page = $_GET['page'];
if ( $page == 'one' ) {
echo 'This is page one!';
} elseif ( $page == 'two' ) {
echo 'This is page to';
}
// http://yoursite.com/index.php?page=one
// Outputs 'This is page one!'
Learn little more about GET request right here : $_GET

This will help you:
Serve multiple pages from 1 PHP file?1
If you want to make something like youtube.com/?watch=XXXXXXXXX
so you need just to change XXXXXXXXX to display multiple content.
but you still can use :
One-page navigation system.
Display / Hides Pages With JS
Example Of JavaScript:
we have website1 as a website
we have a - b - c - d as pages
js will hide all pages exept the page you want to show.

Related

How to put a redirect in front of a php template with a referer check?

I have a template in my (wordpress) child theme folder for a custom post type that I created. It looks like this:
<head>
…
<style>
…
</style>
<script>
…
</script>
</head>
<header>
…
</header>
<?php
…
?>
<div id="content">
<?php xx_page_header($post->ID); ?>
…
</div>
<?php get_footer(); ?>
This is exactly how it is structured. I put it because I am php beginner and if someone is willing to help me, at all, I will most of all need exact information where exactly to put code snippets (which in the end will look simple and small, I guess).
I have 4 pages that are based on this template page. All of them working well.
Here is what I have to add though:
The pages succeed each other containing input forms etc. and are clicked through by passing the forms, sort of a filtering process.
How can I put in front of the template a referer check? So that the pages keep on working their way BUT if someone would copy one of the links and paste it into another browser window he would be redirected always to the first page of them instead of seeing the page of the actual link.
I have made some researches as good as I can and found some pieces I would like to use. As is for:
1) the referer part:
<?php
$ref=getenv("HTTP_REFERER");
$url='url_redirect';
if($ref!='referer_url'){
header('Location: '.$url);
}
?>
2) a regex to make it work for all pages that are using the template:
ref.match(/^http?:\/\/([^\/]+\.)?mywebpage\.com(\/|$)/i)
Let’s say the (filtering) start page is: www.mywebpage.com/startpage/
How do I have to combine those snippets and where to put it (I guess at the beginning of the template)?
I would like to avoid cookies. (using the referer)
Technically, you could do something similar to the code below. Each of your pages after the start page must have unique urls and a token that is passed along on every page. You need to place this code at the very top of your template.
Something similar:
<?php
$url='url_redirect';
if((is_page('template2') || is_page('template3') || is_page('template4')) && $_POST['token'] == '') {
header('Location: '.$url);
}
?>

PHP: Hide/show element when visitors visit certain page

I construct my webpages like this:
<?php
include ('header.html');
?>
<p> bla bla bla </p>
<?php
include ('footer.html');
?>
I use session in the header.html to track the user records. The header.html has got three tags, called logo on the left, search_form in the middle, and login/ sign up buttons on the right
Now, the question is that I would like to hide the search_form DIV in the middle by using php in the header.html file if and when the internet users (not registered members) visit the certain page, e.g., register.php, because this DIV is not necessary.
Note: I would like to control it using PHP, but not javascript or anything else.
Can you help me, please?
Thanks
You could use $_SERVER['REQUEST_URI'] to check if the current url is, for example, 'register.php'. Something like:
<? php
if ($_SERVER['REQUEST_URI'] == 'mysite.com/register.php'):
include ('register_form.php');
?>
Of course, this would only work if the url matched exactly, and query strings would break this. But you get the general idea.
Look into creating sessions and user authentication using PHP.
Change your header.html page to header.php.
in header.php check that if current user as a registered user then show the search div or
check current page by your browser url and make decision whether you want to show search div or not.
You can use session to get current user information in header.php
sample code:
header.php
session_start();
if(isset($_SESSION['current_user'])){ //check register user
// show div
}
else {
$page= $_REQUEST['current_url']; // get current page
if($page == 'register.php' || $page == 'whatever' ){ //check pages where you are not show search page
echo '';
}
else {
//show div
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<?php
session_start();
$_SESSION['view'] = $_SERVER['REQUEST_URI'];
if(isset($_SESSION['view']) && $_SESSION['view'] == '/test/index.php') {
?>
<div>head</div>
<?php } ?>
body here
<div>footer</div>
</body>
</html>
This is my sample work and this is works correctly. If my url not equal to /test/index.php it does not show head. You can set your own logic according to this demo work. I think it can help you.
I tried and tried and found out the solution already. Now, I can share it with you guys:
if( (isset($_SESSION['current_user'])) || (basename($_SERVER['PHP_SELF']) != 'register.php') ){
// show the search_form DIV
}
Thanks for your contribution!

Change the content of webpage when clicking different links

I'm not sure how describe my question perfectly or how to begin. But what i can start with is that i'm making a website in PHP for my school and i've never used php before. And what i need is to only change one part of the page i.e the sidebar. or the content, depending if the user clicks i.e school, contact etc.
on my index.php the sidebar shows the news. But in my navbar if you click i.e useful links then the sidebar should change from showing news to showing a couple of links.
And if it is possible, if you then click on one of the links it should only change the content under the navbar and the sidebar should still show the useful links.
<?php
include('include/header.php');
include('include/nav.php');
?>
<div class=content>
<h1>Heading 1</h1>
<p>Text goes here</p>
</div>
<?php include('include/nyheter.php') ?> <--not part of code! this is the sidebar
<?php include('include/footer.php')
For now, i've only got the news to be shown in the index.php using <iframe>code. but i want to get rid of the <iframe> and use <div class=sidebar">or use ID instead of class.
I've search around to find useful information, but most of them says something about AJAX and JQuery, which i don't know what is or even how to use it =(
If you need more information, just ask as spesific as possible, so that i can give the right information =)
EDIT
I've uploaded the entire website to my google drive so that i could share it with you.
as you may see, i've created a bunch of new .php files (most of them was from some other versions that was created in html, and are now converted to PHP.
The link is: https://docs.google.com/folder/d/0BxsqMLXovlRHdmFlOFYzMjlzRHc/edit?usp=sharing
by the way, the website is in Norwegian.
I hope this information can help you to tell me how to solve my question.
remember, i am totally new to PHP and other programming languages other than HTML & CSS.
You could use if sentence to change the page content according the url
for example
if($_GET['page'] == "about") {
echo "about page content";
}
elseif($_GET['page'] == "page1") {
echo "page1 content";
}
and so on
then you just put links yourpage.com/index.php?page=about into your menu and so on
<?php
// get current page url
$page = basename($_SERVER["REQUEST_URI"]);
if ($page === "index.php"){
// display news info
}else if($page === "usefulllink.php"){
// display userful links
}
?>
Use the php $_GET variable to set which page and navigation you want to use. This is accessed via variables in the url.
For example: www.yoursite.com?page=home&nav=first
You can then include in the php
<?php
$page = $_GET['page'];
$nav = $_GET['nav'];
//set some defaults
if(strlen($page) == 0){ $page = "home"; }
if(strlen($nav) == 0){ $nav = "about"; }
//Then using if statements you can select content and navigation
if($page == "home"){
//print navigation bar with selected link
if($nav == "about"){
//print content for the selected navigation
}
}
?>

How to make php includes for html pages in web site

I'm the first to admit that I'm green when it comes to PHP coding. However many years ago, a colleague gave me a pattern to use for joining PHP with HTML to create web pages. Now, I am looking to revamp the site but I want to know if there is a better way to write it? Currently, I have an index.php page which has a layout similar to this:
<?php
if (! isset($HTTP_GET_VARS['content']) || ! $HTTP_GET_VARS['content']){
$content="home";
}
else
$content=$HTTP_GET_VARS['content'];
//1
if ($content == "home"){
$page_title="Page Title";
$keywords="Keywords found in Meta";
$desc="Description found in Meta";
$style="scripts/style.css";
$popupjs="none";
$storbutnjs="none";
$retreatjs="none";
$rolloverjs="scripts/rolloverjs.js";
$readform_chkjs="none";
$logo="req-files/logo.html";
$sidebar="req-files/sidebar.html";
$main="home.html";
}
//2
if ($content == "about"){
$page_title="Page Title";
$keywords="Keywords found in Meta";
$desc="Description found in Meta";
$style="scripts/style.css";
$popupjs="none";
$storbutnjs="none";
$retreatjs="none";
$rolloverjs="none";
$readform_chkjs="none";
$logo="req-files/logo.html";
$sidebar="req-files/sidebar.html";
$main="about.html";
}
include ("req-files/head.html");
include ($logo);
include ("req-files/navbar.html");
include ($sidebar);
include ($main);
/*include ("scripts/analytics.js");*/
include ("req-files/footer.html");
?>
So, if a person typed http://yourwebsite.com/?content=about They would get the whole About page built in the browser with all required meta, header, sidebar, footer, javascript, css, analytics, etc. Each of those required parts are html files, some may have php scripts for some of the $ callouts like Page Title, Keywords, etc.
One of my problems is when my client wants to change the name of one of the '($content == " ")' to something else. First, I can change the variable, but then I have to redirect the old name to the new name so that we don't lose page ranking.
For instance, http://yourwebsite.com/?content=about needs to be redirected to http://yourwebsite.com/?content=about-us.
Eventually, the client will redirect all or most pages to be more direct, http://yourwebsite.com/about-us. It is believed that this will make the rebuild go more smoothly when the site is turned into a WordPress website.
So, is there a better way to write this? Is there a better way to redirect URLs?
Thank you...
$HTTP_GET_VARS is deprecated. Please try to learn PHP from the official docs.
To answer your problem, another commonly used system is like this:
File: include.php
<?php
function topbanner($title) { ?>
<!doctype html>
<html>
<head>
<title><?php echo $title; ?></title>
<script type="text/javascript" src="jquery.js"></script>
</head>
<body>
<header>
Site name, Logo, etc.
<header>
<?php }
function footer() { ?>
<footer>
©2012. Your company name. Best viewed in Mozilla Firefox.
</footer>
</body>
</html>
<?php }
Now, create html pages as you would normally do, but make sure the extensions are .php. In those pages, do this.
<?php require_once('include.php'); topbanner("Title of this page"); ?>
<h3>Welcome to this site</h3>
<p>Content content content</p>
<img src="image.jpg" />
<?php footer(); ?>
This is for simple pages. If you need more complex setup, follow the style of fork-cms to redirect pages using .htacess. Either way, pages are renamed means they lose indexing. Why do pages need to be renamed often?
http://php.net/manual/de/function.file-get-contents.php
so you can include html sites in php (var)
$page = file-get-contents('myHTMLSite.html');
and
str_replace('{header}', $header, $page);

Can we have a totally new page with php when click on the new button?

I have a site lets name it http://mysite.com/index.php which is my main site with all the information and I want to have another page which is something like http://mysite.com/index.php?=premium and in this premium page will be totally new whole page. Which means not the same title or not have anything relate to the index.php page.
The reason is that I don't want my visitors to go directly to the premium page so they have to come to the index.php page first then click it from there.
Is that possible this way?
#knittl well not sure if my question was cleared
but what is actually happening now that the above code can get me to the premium page fine with no problem
but inside the premium page include many div class and I have this in the premium page
<?php
if(!isset($_GET['ch'])){
$ch = 1;
} else {
$ch = $_GET['ch'];
}
if ($ch == 1) {
echo "<div class='player_live_server_info'>
so what happen now that I have this
<li><img src="live_img/ch_5.jpg"></li>
I don't know if I do it right or wrong but It just redirect back to the normal page.
<?php if(isset($_GET['page']) && $_GET['page'] == 'premium') {
// this is the premium version, only visible when called as index.php?page=premium
?>
<html>
<head><title>you are on the fancy premium page</title></head>
<body>
<h1>your premium page</h1>
</body>
</html>
<?php } else {
// this is the normal page with a link to the premium version
?>
<html>
<head><title>you are on the lame normal page</title></head>
<body>
<h1>your normal page</h1>
<p>
click here to go to our premium page
</p>
</body>
</html>
<?php } ?>
$_GET['something'] is the best choice for you..you can also use $_SESSION, but that will be complicated plus make your application slower.

Categories