PHP Change Page? - php

I have found out the bad news about header(), so I am no longer using it, because now my website doesn't work..
Is there any other simple way to change pages automatically?

you mean like redirect?
you can also use this:
<meta http-equiv="Refresh" content="5;
URL=http://www.yahoo.com">

Try javascript redirection
<script type='text/javascript'>
window.location = "new_page.php";
</script>
or you can use meta-refresh (many examples in google).
If you can't use header in your script, just add ob_start(); at the beginning and then you can use it AFTER html.

Ultimately you'd do well to re-work your code so you do a redirection before sending a bunch of HTML that's never going to be seen. And if you do that - you can use header(), which causes less delay for your end users, reduces processing all round, and is search-engine friendly! Win-win-win.

Related

check if javascript is available and redirect if javascript is not available [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to detect if JavaScript is disabled?
In my php application i need to check whether the javascript is turn on or not in browser. I have tried this <noscript><p>javascript is off<p></noscript> it's working fine.But i need to redirect to a page if javascript is OFF so developed like this
<noscript>
<?php header('Location: index.php');?>
</noscript>
But i/ts always redirecting to index.php, there is any way to do this.
your solution clearly cannot work, since php is executed before the page is served on client
you could instead do something like
<noscript>
<meta http-equiv="refresh" content="0;URL='http://example.com/'">
</noscript>
in the head of your document
<noscript><meta http-equiv="refresh" content="1;url=error.html"></noscript>
try this to redirect if JS is disabled
Php script runs independently of js
There are known issues with noscript
Instead do this:
<html>
<head>
<script>
window.location.href="javascriptEnabled.html";
/*Since javascript is enabled redirect to javascript based page*/
</script>
</head>
<body>
Showing Static Website since javascript is not enabled..
</body>
</html>
There is not a good way to do it, because without client side interactivity, you can not get information back from the client.
You could do something like this...
Set up a meta redirect to trigger after 5 seconds
use javascript to override the meta redirect
Therefore, if javasccript is enabled, you get to the javascript page, otherwise the non javascript page
It does redirect to index.php because the <?php header('Location: index.php');?> is processed by the server while the <noscript> is processed by the client ..
A correct way to do this is to set a cookie using javascript on the client, then check for the cookie's presence with php on the server, If the cookie does exist, then javascript is on, Else, it's off.
You could use a different approach here
Use the no javascript version as default version
Use a javascript redirect to the enhanced version
Remember it's better to not have separate versions of the site. You could progressive enhancement with libraries as http://modernizr.com/ or https://github.com/filamentgroup/enhance

Can I load javascript from a php file?

Can I do something like this?
<script src="/js/custom-user.php" type="text/javascript"></script>
The reason behind it is that I want the .php file to die() when the user is not logged in, so that other visitors (not authenticated) cannot see what the javascript looks like. Is it possible/safe to do like this?
Yes, but I do have two recommendations. First, it is better, in your circumstance, to only output the <script> if the user is logged in. Seriously, you don't want the thing which is outputting you js to really know or care about whether the user is logged in.
If you do output js in PHP, then you should include the appropriate header:
header("Content-type: text/javascript");
// either readFile or custom stuff here.
echo "alert('i canz have data!')";
// or, if you're less silly
readFile('/path/to/super-secret.js');
Actually, I once had CSS output by PHP (oh, you can do that too) which completely changed based on the get variable. I literally could have:
rel="stylesheet" type="text/css" href="css.php?v=#FF0000">
And it would use #FF0000 as a base color to completely re-define the color schemes in the website. I even went so far as to hook it in to imagemagick and re-color the site logo. It looked hideous because I'm not a designer, but it was really neat.
Certainly, so long as the php file being reference sends the appropriate content-type header when being downloaded.
Yes, you can do this, and it is safe.
In custom-user.php you will have to set a proper Content-Type header:
header('Content-Type: text/javascript');
And then output the javascript:
readfile('script.js');
Yes, but... You should better do it like this:
<?php
if ($loggedIn) { echo '<script src="/js/custom-user.js" type="text/javascript"></script>'; }
?>
That would prevent loading of empty file. All functions should be put in outer file, if you want some specific javascript changes, make a code in HEAD SCRIPT
Yes, that will work.
That's how JavaScript minifiers are able to dynamically serve minified scripts. (e.g. http://code.google.com/p/minify/)
You can but it will slow down your pages since every time someone accesses your page modphp will have to run your php/javascript script.

How can I redirect using a PHP script called from an SSI?

On a WAMP server, I have a server-side include in a file, a.shtml, composed of the following code:
<!--#include virtual="./req.php"-->
The contents of req.php are:
<?php
Header("Location:index.php");
echo "still here";
?>
When I open a.shtml, I see the text still here, but the page has made no attempt to redirect itself. Why is this? And is there any way to make it work?
Thanks for the help
EDIT: The reason I want to do this is because I have some session variables that I want to influence the way the PHP script acts. If the session variables are not set, I need it to redirect to a login page. I know I can just write the entire thing in PHP, but I'd like to do it this way if possible. If it's not possible to change header information from an included PHP file from SSI, then I'll just do it entirely in PHP.
it's impossible
you don't need that.
just address tour script that set session variables directly, not through ssi
MAYBE (with capital letters Lol), you can pull this off if you call that script in an IFRAME and that IFRAME outputs some JScript like window.parent.location = <some_url_here> forcing its parent to change its location... Its just fast-thinking from my part, I might be wrong with IFRAMEs' parent-child relation to the original document, as I haven't tested the "idea" myself :)
If your req.php returns the following html code, the redirect will happen:
<html><head>
<title>HTTP 301 This page has been moved</title>
<meta http-equiv="Refresh" content="0;URL=https://www.example.com/index.php">
</head>
<body></body></html>
But: "Google Warning: Using The Meta Refresh Tag Is Bad Practice"

PHP Header Location Change of Parent Frame

I have an iFrame that does some background processing. When this processing is complete I would like to re-direct the user to another page, but the header change code is only affecting the embedded iFrame. Is there a way to target the main window?
I have seen the deprecated Meta redirect have a target attribute, but I don't know how widely it is supported.
In Javascript:
top.location.href = "resultpage.htm";
Works only if the top frame is on the same domain as the emitting page.
For a solution that works across domains and without Javascript, the following would work:
Continue
Use JavaScript to track content of frame, if content change, redirect browser :)
We can use javascript like this:
target.window.location='locationpage.php';
parent.window.location='index.php';
For me this always works without fail, have tried many of the others but there always seems to be some sort of issue....and it does not matter if headers have been sent etc.....
<?php
echo "<script>window.location = 'http://www.google.com'</script>";
?>
Remember this goes at the very bottom

Creating visible redirect?

Please Pardon if the question sounds silly, but nevertheless its a question which I want to know. :)
How can I redirects which display that you are being redirected (like older Gmail and LinkedIn). Whenever I tried to do that, I got errors saying that Headers were already sent.
Then somebody here told me that I should not output any markup before redirection (like facebook login). So how do I go about it and display something nice during redirection??
you want to use meta redirects. they enable you to show a page, and after a few seconds this page will send you to the new page. all you have to do is add a tag to the portion of your 'something nice' redirection page.
here's a quick tutorial on meta redirects:
http://webdesign.about.com/od/metataglibraries/a/aa080300a.htm
you want to do something like this:
<html>
<head>
<meta http-equiv="refresh" content="2;url=whereto">
were 2 is the number of seconds to display your page and whereto is where you want to send your user
You need to output your page, which will include the following META keyword:
<html>
<head>
<!-- redirection to example.com in 5 seconds -->
<meta http-equiv="refresh" content="5;url=http://example.com/" />
...
</head>
<body>
...
Read the following article for more help: http://en.wikipedia.org/wiki/Meta_refresh
Those redirections are not done via "normal" redirection HTTP headers. Instead they display a page and use either a META Refresh or some Javascript to navigate to the new page.
Personally I find both methods not very nice, both for users and for search engines. Using HTTP headers that also signify why there is a redirect (Permanently moved, temporary, &c.) are way better imho.
If you are getting the headers already sent then that means you were trying to redirect with a PHP header() redirect and you had output on the screen before calling the header() function, 1 solution to that is to use PHP's Output Buffering which will then allow you to call the header() redirect anywhere on the page and not get that error.
To show a message you could use the meta method mentioned in some other answers maybe even throw in a javascript redirect with it, just to be safe however the way I would do it would be something like this below
<?PHP
ob_start();
//show a message
echo 'You will be redirected in 10 seconds';
//amount of time to show the message above before redirecting
sleep(200);
//redirect
header('http://domain.com');
?>
I did not test this but you should get the idea, actually now that I think about it, sleep() might not work correctly as expected with output buffering

Categories