Replace page content before file loads? - php

I wanted to know, is there any sory of scripting that I can use so that I can simply change one little thing and then the content will change for multiple pages? Maybe PHP or the htaccess file.
What I want to do is make an "Under Construction" page for my website, that I can periodically turn on or off. Right now, the method I am using is JS, which is not too effective. What I have it doing is it will replace the body tag's content with that of the under construction pages, and then change the title to be "Under Construction". The problem with this, the function loads after the page has loaded. So I either need a JS script that will load before anything on the page does, or a php script that does something near the same thing. I also thought (if it was possible) the htaccess file would be really nice too, because I could apply it to certain directories. I know I can use the htaccess file to redirect the user (and I can do that with PHP and JS too) but I do not want to do that because I want the url be stay the same. If I were to redirect the user from page1.html to underconstruction.html, then that changes the url in the browser. I want the url to stay as page1.html for page1, page2.html for page2, and page3.html for page3... Does anyone know how I can accomplish such a task? If so, please help.
Thank you!

If you use the following snippet in an htaccess file, your url does not change but redirects:
RewriteEngine on
RewriteRule ^(.*)$ ./under_construction.html [L]

Sure thing there is lets take ux9i example and re-write it slightly:
// Set this to true to rewrite the page
var underConstruction = true;
function rewritePage(){
if (underConstruction){
document.getElementById ('entirePage').innerHTML =
"<h1>Under construction</h1>";
}
}
Test.html
<html>
<head>
<script type="text/javascript">
window.onload = rewritePage;
</script>
<title>write example</title>
<script type="text/javascript" src="UnderConstruction.js"></script>
</head>
<body>
<div id="entirePage">
<p>Some document content.</p>
</div>
</body>
</html>
This ought to do the trick, if I were you I would be using redirection instead of this, here is what I mean:
// Set this to true to rewrite the page
var underConstruction = true;
function rewritePage(){
if (underConstruction){
location.href = 'contruction.html';
}
}
Leave the html as it is. Cheers
EDIT
Here is how to do it in php, you'll make page called lets say "construction.html" , now inside of your php page at the top place this code
<?php
$redirect = 1;
if($redirect == 1){
header('Location: construction.html');
}
?>
It will directly transfer/redirect you to the construction page in case redirect is set to one .. after you're done with the construction page either remove this portion of code completely or set redirect to any other number beside 1.
Sorry I just read this part
If I were to redirect the user from
page1.html to underconstruction.html,
then that changes the url in the
browser. I want the url to stay as
page1.html for page1, page2.html for
page2, and page3.html for page3...
Does anyone know how I can accomplish
such a task?
You should let other people know when you edit your question, you should use then iframe, try to google what is iframe and how to use it. Then you can apply the same logic described above to iframes as well.

Have all your pages include this script.
UnderConstruction.js:
// Set this to 1 to rewrite the page
var underConstruction = 0;
function rewritePage ()
{
if (underConstruction)
{
document.getElementById ('entirePage').innerHTML =
"<h1>Under construction</h1>";
}
}
Test.html:
<html>
<head>
<title>write example</title>
<script type="text/javascript" src="UnderConstruction.js"></script>
</head>
<body onload="rewritePage()">
<div id="entirePage">
<p>Some document content.</p>
</div>
</body>
</html>

Related

Interval refresh a static request

I apologize if this seems stupid or redundant, I've search and read related pages with little understanding.
I use this function to call my chat widget to each page. (In case I would like to switch chat server.)
<?PHP include "newchat.php"; ?>
I would like to refresh newchat.php at an interval of 20 minutes. (To prevent chat time out.)
I use this code on newchat.php, which results in the entire main page to refresh. (ie. index.php)
<html>
<head>
<script type="text/JavaScript">
<!--
function timedRefresh(timeoutPeriod) {
setTimeout("location.reload(true);",timeoutPeriod);
}
// -->
</script>
</head>
<body onload="JavaScript:timedRefresh(10000);">
*chat script here*
I think I may need to put script/ajax on each template page, which tells browser to refresh only that element, however I do not understand this code and am not sure if it applies.
Thank you for reading and help you may provide.
Try putting your newchat.php file into a iFrame on your pages instead of directly including it. ie:
<iframe src="newchat.php" id="chatFrame" frameborder="0" width="YOUR-WIDTH" height="YOUR-HEIGHT">
Then you can refresh from with-in the newchat.php file. Or if you want control over it from each page you can use the iframes id to control a refresh from the parent:
document.getElementById('chatFrame').contentDocument.location.reload(true);

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);

Use page title as text in body

Im trying to find a way to display the title of the generated page in the body as text.
I have tried using the following code to display the title.
<script type="text/javascript">
document.write('You have visited the page ' + document.title );
</script>
Although the code does what i want, i think that i must use another way to do it, more SEO friendly! When i open the page and check the code, i see this code and not the result of (document.title).
Add the title to the server-side script that generates this page. You'll need to post some of your server-side code to get a more specific answer. In general:
<?php
$title = "My Page"
echo <<<EOT
<html>
<head>
<title>$title</title>
</head>
<body>
You have visited the page $title
</body>
</html>
EOT
?>
Javascript does not affect the source code of the page. It gets rendered after the page loads and you see the result. Use PHP for sorting like this.
Any client side generated html isn't going to appear in the source. You need to do it server side, in PHP or whatever, if you want it in the source.

HTTP Referrer on Redirection

I have a small script that redirects users to main site if they come from a banner on my/other remote sites.
<?
.
..
...
....
header("location:$golink");
?>
But google analytics will not show the referrer site (where the script is working) instead it shows the url where the banner is clicked. Obviously I can not keep a track of all sites where banner appears and dont want to. I want the refferer to be the site where the script is working. How do I have to use the $_SERVER['HTTP_REFERER']; in order to do this ?
GA has a method that will let you to override the default referring URL (document.referrer) with a specified value.
So if you want to keep the redirect server-side, you can append the referring URL as a query string param in your header() call, and then look for it on the target page and specify it as the referring URL.
I don't know how you are building your $golink variable, but basically you would add something along the lines of:
$golink .= "?ref=" . $_SERVER['HTTP_REFERER'];
Use a & instead of ? if there are already URL params, and the code above assumes using ref as the URL param, so use whatever var you want.
Then on your target pages, before your _trackPageview call, you would add
_gaq.push(['_setReferrerOverride', ref]);
ref would be a javascript variable with the value of the ref=xxx query string param. For some weird reason Javascript does not have a native way to grab URL param values, nor does GA provide an (exposed) solution. If you already have a solution on your pages for grabbing URL params (like something from a framework or a function you've already made) then use that. Otherwise it's pretty easy to find a javascript function that will do it for you.
There are a couple benefits to doing it this way:
You don't have to worry about the visitor seeing an interstitial page.
You don't have to worry about GA not getting a chance to fully load before redirect
You can see the referrers tied directly to your landing pages, because with the interstitial page, you will always see that interstitial page as the referrer, and will have to look at referring url reports for the interstitial page.
Yes, G.A is blind to this kind of server-side stuff. And their PHP Api is not helpful either.
However, you could have a short redirection page, holding the GA tag inside like this :
<html>
<head>
<title>A web page that points a browser to a different page after 2 seconds</title>
<meta http-equiv="refresh" content="2; URL=<?php echo $golink; ?>">
<meta name="keywords" content="automatic redirection">
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
</head>
<body>
If your browser doesn't automatically go there within a few seconds,
you may want to go to
the destination
manually.
</body>
</html>
Notice the $golink variable in the meta tag.
If you use this, do not forget to replace UA-XXXXX-X by your real account number.
Credits : optimized GA tag goes to Mathias Bynens
[EDIT : javascript only version]
<html>
<head>
<title>Redirecting you...</title>
<script>var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.src='//www.google-analytics.com/ga.js';s.parentNode.insertBefore(g,s)}(document,'script'))</script>
<script>
<!--
if (window.addEventListener)
window.addEventListener('load', function() { window.location="<?php echo $golink; ?>"; }, false);
else
window.attachEvent('onload', function() { window.location="<?php echo $golink; ?>"; });
// -->
</script>
</head>
<body>
</body>
</html>

Php Location("some other page")

I'm new to PHP and this is something that I don't know how to do, even though I have been searching it.
I know that redirecting can be made with Location("some page"). I also read that this works just if there is nothing displayed to user.
What I want to do is:
Display a message to user. echo "message.redirecting...."
Wait for 2 seconds sleep(2);
Then redirect Location("some page");
Any ideas?
Andrew
This is part of an assignment and javascript is not allowed. Only PHP.
You can use a meta refresh, which is just a html meta tag placed inside the <head> of your page. Like this:
<meta http-equiv="refresh" content="2;url=http://newurl.com/">
This will redirect the page to the http://newurl.com after 2 seconds.
Do not do it this way.
It's VERY bad usability.
And there is not much sense in saying "redirecting".
That's legacy of ancient ages of raw HTML sites. No good site using such redirects these days.
Redirect with no messages.
Unfortunately you can't do that. header() calls, such as header('Location: '); rely on http headers, which have to be sent before any output is sent to the client.
I reccomend using a Javascript Redirect if you want a message displayed to the users.
<html>
<head>
<script type="text/javascript">
<!--
function delayer(){
window.location = "../javascriptredirect.php"
}
//-->
</script>
</head>
<body onLoad="setTimeout('delayer()', 5000)">
<h2>Prepare to be redirected!</h2>
<p>This page is a time delay redirect, please update your bookmarks to our new
location!</p>
</body>
</html>
or you can do a php redirect like this:
<?php
header("Location: http://www.example.com/"); /* Redirect browser */
?>

Categories