Trying to load my first php site in a browser; Firefox displays odd coding tags, IE asks if I want to "open" the file, instead of loading the page. I think I copied a tutorial 100%, so I suppose it really should work.. Is there anything else I need to do to make this work, or may I have some code error?
functions.php
<?php
function displayContent($content) {
echo implode($content);
}
function displaySidebar($sidebar) {
echo implode($sidebar);
}
?>
template.php
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>index</title>
<meta name="author" content="SS" />
<link href="site.css" rel="stylesheet"type="text/css" />
</head>
<body>
<div class="container">
<div class="header">
header
</div>
<div class="sidebar">
<?php displaySidebar($sidebar); ?>
</div>
<div class="content">
<?php displayContent($content); ?>
</div>
<div class="footer">
footer
</div>
</div>
</body>
index.php
<?php
//include our functions
require_once ('functions.php');
// declare arrays
$content=array();
$sidebar=array();
//handle page request
$t=$_GET['t'];
if($t == 'about') {
//display about page //can we load a full template?
$content[]="<h2>about</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
} else {
//display the home page
$content[]="<h2>home page</h2>";
$content[]="<p>Content</p>";
$sidebar[]="";
}
//Important this is on bottom
//I guess content must be ready on load.
include ('template.php');
?>
OUTPUT: (Firefox)
about"; $content[]="
Content
"; $sidebar[]=""; } else { //display the home page $content[]="
home page
"; $content[]="
Content
"; $sidebar[]=""; } //Important this is on bottom //I guess content must be ready on load. include ('template.php'); ?>
Uh, have you already installed Apache and PHP, and have you put the files in Apache's web root? Because PHP is not HTML: it's not parsed by the browser. It's executed by the server, which then returns the HTML response to the browser.
If you are working locally, try installing wamp or xampp, if you're working on a host. I suggest looking for one that supports php and mysql.
It could be that your server doesn't have PHP set up, or maybe your files aren't ending in .php.
To check, I'd remove what you've done so far, and make a new file called index.php. In it, I'd just write:
<?php
echo "Hi!";
?>
Then go to the page. If that works, then you know your script has some issue, if not, then you know the server isn't set up correctly.
If that does work, you may want to change echo "Hi!"; to say phpinfo();. That tells you all about the PHP install, which may come in handy to know at some point!
Related
I am using one header file for every page which will show the HTML head(which includes meta tags and other CSS links)
Here I have used everything as dynamic as if I like in the case of canonical tags I have used $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_HOST'] and all other links to CSS are also accessible even if it can be any file from any directory.
Now I want to create a one-time title meta tag & description tags like this will be my main file looks like
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page_title; ?></title>
<meta name="description" content="<?php echo $page_content; ?>" />
//Other meta tags & linking to css & js files
</head>
<body>
// Content ........
</body>
</html>
Now here is the twist is that many people will say that just use
$page_title = "Here is the title of the specific page";
$page_content = "Here goes the long-form description describing the page of specific page";
Yes, this would have worked out if the pages were different and had the same place to put that all before including the header file.
But my index page looks like this let me explain it too. (Using Bulma as a framework)
<?php
require 'include/db_connect.php';
require 'include/header.php';
?>
<form name="submitform" method="POST">
<div class="columns is-multiline" id="wrapper">
<div class="column is-6-desktop is-12-tablet" id="main_content">
<div class="box">
<?php
if($country)
{
?>
<?php require 'inc/country.php'; ?>
<?php
}
else if($country && $state)
{
?>
<?php require 'inc/country_state.php' ?>
<?php
}
else if($country && $state && $district)
{
?>
<?php require 'inc/country_state_district.php'; ?>
<?php
}
else
{
?>
<?php require 'inc/other_than.php'; ?>
<?php
}
?>
</div>
</div>
</div>
</form>
<?php require 'footer.php'; ?>
</body>
</html>
The main point here is that I am using the dropdown button which gets auto-submitted using js that's not relevant here but from the top, I have just explained what is the structure of my code.
Now as you can see the if-else structure which includes other files that create dynamic pages but their code starts only from the body and not from the head directly so I am not able to add those title tags and descriptions.
Now how to add title & description tags uniquely to each of these pages.
Any solutions, please... Thanks in Advance
Like already written in the comments. You should define the vars ahead of require 'include/header.php'; or use an MVC structure.
If you still want to stay with the existing code ob_get_contents() might help you.
Use ob_start(); at the beginning to tell PHP to not print any output and instead write the output to a buffer.
You can then at a later point use ob_get_contents(); to fetch the output buffer and print it.
You have to search in the output buffer for an identifier like %page_title% then and replace it with the actual value before sending the output.
This may look like following:
echo str_replace(%page_title%, $page_title, ob_get_contents());
Still I'd rather suggest restructuring your code, as the solution with output buffer is slower, uses more memory and is poor to debug.
I've run into problem whereby I'm not able to include a header file into a PHP file
In the code below, I've removed the original content as it's quite big & anyway what the code does is irrelevant to including the header file
MAIN.PHP
<?php
php content
?>
<html>
<head>
</head>
<body >
<div>
header content
</div>
<br/>
<div>
</div>
</body>
</html>
I would like to have the following code in the header.php file & include it on all pages.
<div>
header content
</div>
When I do so with the main.php file looking as below, the page draws blank.
<?php
php content
?>
<html>
<head>
</head>
<body >
<div>
<? php include('header.php') ?>
</div>
<br/>
<div>
</div>
</body>
</html>
I've tried debugging this & following a few solutions on stackoverflow, but evidently have failed to achieve what I'm after.
Could I please request a second pair of eyes to help me spot the problem?
Well, first of all, I don't think you copied the code correctly to here.
As my debugging eyes can see, you got 2 issues:
Line 1: < is missing at the start. Change it to <?php. (Maybe
copy paste error?)
<?php include('header.php'); is missing closing tag. Change it to:
<?php include('header.php'); ?>
If you get a blank page, you probably have some errors. Try finding the "error.log" file in you working directory or add the following code at the start of the page:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
I am currently working on a php project in which i needed to implement a page redirection.In HTML the page redirection
<a href=abc.html#section>Pagename<a/>
works proper to get me to any section of the page. But in php i tried to implement the same with tag and include method which gives me error.the code i tried is
if(?tag==tagname)
include('pagename.php#section');
How can i redirect visitor to some other page on some section which is in middle of the page?? implementing abc.html#section in php. Please give your guidance. Thanx in advance.
The structure is
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<php>
if(?tag==home)
include(home.php);
</end php>
<footer>
</end body>
</end html>
This works fine but it will always open new page from the top. I needed to take users to home.php#section. Please help me out.
use $_GET to access query-paramters.
e.g.:
index.php?foo=bar&some=thing
$_GET['foo']; //contains bar
$_GET['some']; // contains thing
<html>
<body>
<header>
<Navigation menu>
<span>Home</span>
</end Navigation menu>
<?php
if(isset($_GET) && $_GET['tag'] && $_GET['tag'] == 'home') {
include(home.php);
}
?>
<footer>
</end body>
</end html>
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>
For the last few days I am wandering arround the web for a script which would have the elementss I wish for.. The best thusfar is this one http://www.queness.com/post/328/a-simple-ajax-driven-website-with-jqueryphp, but there are a lot problems, where i need better solution:
I want no hashtags: I want all links for loading pages like folder (www.site.com/first/site/). This works magicaly on github (click here https://github.com/spjoe/deluge-yarss-plugin, and click on a random file and go back while watching adress bar). If someone knew what or how are they doing it would be really cool.
I need dynamic content: The script above only loads "cases" from switch function, while I need the php to check the url and find a file in mysql database and echo out content from desired tables based on the url, if it is found in the db ofcourse (if I point a link to site.com/harvey the php should find Harvey in the db and display for example his phone, adress and other details, while this must not be preloaded but requested only on click due to bandwight issues).
History: I would like to have an option where back and forward movements on the site would work flawlessly and with the animation as it works while clicking links..
Working links inside changable div: Because there is a problem with some scripts which don't change content where the link is inside it (for example first content box includes navigation, which should be replaced with different navigation in the second box), again this work very well with github..
I hope you give me some links or even more help with some code..
I think the script you are looking for is pjax, it has exactly all the features you want. You can find it on github: https://github.com/defunkt/jquery-pjax/tree/heroku
EDIT
You can use it with any server-side language you like. For example,
<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script src="jquery.pjax.js"></script>
<script type="text/javascript">
$(function(){
$('nav a').pjax('#content')
})
</script>
in your website's head,
<nav>
<a href="otherContent1.php" >click me for other content</a>
<a href="otherContent2.php" >click me for even more additional content</a>
</nav>
<section id="content">
<h1>Original Content</h2>
<p>
this will be replaced by pjax with the content's of otherContent1.php or otherContent2.php
</p>
</section>
in your main template and modify your php code so that it looks for the HTTP_X_PJAX header. If it's present (see xhr.setRequestHeader('X-PJAX', 'true')) render only the part that should replace the contents of <section id="content">, else render the whole page. Pjax is then intelligent enough that if it works, it will replace only the contents of <section id="content">, and if it doesn't work, you still have regular links working. pjax will even take care of google analytics, if you're using it, so your tracking is still working.
EDIT2: Example
Ok, here you have a sample php page. be aware that I did not test this, i just wrote it quick and dirty here on stack overflow to show how you could solve this. This code is untested and certainly not production ready. But as an example, you could do something like this (the file should be named index.php):
<?php
$render_template=true;
if ($_SERVER["HTTP_X_PJAX"])
{
$render_template=false;
}
?>
<?php
if ($render_template) {
?>
<html>
<head>
<meta charset="utf-8" />
<title>Testing Pjax</title>
<script src="jquery.js"></script>
<script src="jquery.cookie.js"></script>
<script src="jquery.pjax.js"></script>
<script type="text/javascript">
$(function(){
$('nav a').pjax('#content')
})
</script>
</head>
<body>
Date in template: <?php echo $today = date("D M j G:i:s T Y"); ?>
<nav>
<a href="index.php?content=1" >click me for other content</a>
<a href="index.php?content=2" >click me for even more additional content</a>
</nav>
<section id="content">
<?php
}
?>
<?php
if ($_Get["content"]==1)
{
?>
<h1>Content=1</h1>
Date in content: <?php echo $today = date("D M j G:i:s T Y"); ?>
<p>
this will be replaced by pjax with the content's of index.php?content=1 or index.php?content=2
</p>
<?php
} else {
?>
<h1>Content 2=2</h1>
Date in content: <?php echo $today = date("D M j G:i:s T Y"); ?>
<p>
this will be replaced by pjax with the content's of index.php?content=1 or index.php?content=2
</p>
<?php
}
?>
<?php
if ($render_template) {
?>
</section>
</body>
</html>
<?php
}
?>
If you want the URL to display the "Correct" url when you ajax a page request you can use PJAX it changes the history with pushState but it doesn't work on ie6-ie9 check out CanIUse: History for compatibility issues.