View Full Version : ECHO does not work in php
mcolton
03-10-2011, 11:04 PM
Edited: I found out that I cannot echo at all. I tried a small program below and when I run it, I get a blank page.
<html>
<body>
<?php
echo 'You will be re-directed in 3 seconds';
?>
</body>
</html>
The original problem is below:
I have a piece of code below and when I get to this condition, I want to print "You have already registered" and then return to the page shown in the "header" line
Before the following php code, I have checked mysql and either found 1 or 0 records. If I find 1 record, that is when I want to alert the user he is already registered. Thanks for any help
if ($num==1) {
mysql_close();
echo ' you are already registered' ;
header("Location: http://www.lotatennis.com");
}
Schmoopy
03-11-2011, 06:39 PM
Are any functions working for you?
Try doing:
<?php
phpinfo();
?>
That should print out a lot of information about PHP and your setup.
If it doesn't, then there's probably something wrong with how your server is set up.
bluewalrus
03-11-2011, 07:05 PM
Also no output can be displayed if you are using a header.
echo ' you are already registered' ;
header("Location: http://www.lotatennis.com"); // this wont execute with the echo
mcolton
03-11-2011, 07:25 PM
phpinfo didn't show anything.
I took the "header" line out and still nothing.
I know I'm getting to this test file because I put a line of text in there:
<html>
<body>
text
<?php
phpinfo();
?>
</body>
</html>
When I ran this, I got "text" and thats all. If I take the "text" out, I get nothing. I KNOW PHP is working because the whole rest of my code IS working.
Beverleyh
03-11-2011, 08:22 PM
Is the file that this code is in a php file? ie - does it end with the .php extension
mcolton
03-11-2011, 08:35 PM
no. it is a html file
Beverleyh
03-11-2011, 09:53 PM
There's your answer ;)
mcolton
03-11-2011, 11:54 PM
I don't understand. That little test file is html. My original code that is not working (below) is in a php file:
if ($num==1) {
mysql_close();
echo ' you are already registered' ;
header("Location: http://www.lotatennis.com");
}
djr33
03-12-2011, 12:42 AM
Beverley is saying that PHP will only execute when the extension is .php, and never on an .htm or .html page.
(Technically you could reconfigure your server, but that's not relevant here.)
For EVERY page on which you are trying to run PHP, is the extension .php? That's step 1. Then try the phpinfo() code again.
Bluewalrus mentioned it, but you may have missed it;
this won't work:
echo ' you are already registered' ;
header("Location: http://www.lotatennis.com");
the header will fail because you've already sent output (the text) to the browser.
if you switch the order:
header("Location: http://www.lotatennis.com");
echo ' you are already registered' ;
then the echo won't be seen, because the user has already been redirected to the other page.
If you want to do this (show a notice and then redirect), it has to be done on the client side; there's no way to do it from the server. use javascript or a html header.
djr33
03-12-2011, 03:00 AM
It's actually a little more complicated than that.
If the redirect works, you won't see the page (or the echo). That's true.
But the redirect probably won't work. Headers must be sent before any text output of any kind or they don't function. The server sends two parts for every page: headers then content (html). So if you have anything before the header() command, it won't work. This includes an echo or anything outside of the <?php ?> blocks.
Of course once you reverse those two statements then you will have the problem that traq is describing so you may need to re-design the system.
bluewalrus
03-12-2011, 06:18 AM
If you want to do this (show a notice and then redirect), it has to be done on the client side; there's no way to do it from the server. use javascript or a html header.
This is for once you have the php in a php file. You could put a GET value into the header redirect.
header("Location: http://www.lotatennis.com?user=a_registered");
Then on that page put
if ($_GET['user'] == 'a_registered')
echo "You are already registered.";
mcolton
03-12-2011, 11:57 AM
Thanks guys.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.