hi
This is structure question about where I place my chunks of PHP code so my call to the Header() fuction works.
I have an HTML page derived from an HTML Dreamweaver template (so it has a chunk of HTML at the top i.e. head, body tags, and a nav menu). The page has a PHP script to get the users userid and password from a form. The script goes and checks a sql dbase to ensure the user has entered the correct info. If he has, I want to use the Header(Location: "Page2...") to take him to my next page, e.g. Page2 I get the known error "cannot redirect, headers already sent".
Now, I know I get this error because I am violating the rule which says I must not send any kind of output prior to the Header call, but my problem is what structure should I use to comply with that rule? On my Page1 I must have the HTML ahead of the Header call else how would I be able to display the form?
I could maybe set up a page called Page2 with just the PHP code only to check the form variables and call the Header function but how do I call that page into the browser from Page1 so it can execute? As Page1 will have the HTML for the form and call to go to Page2 with Header( function will fail. I believe I cannot use include to jump to Page2 because that also violates the 'no output' rule... It seems an unsolvable conundrum to me, like a chicken & egg situation! I'm sure one of you gurus have the solution for me! many thanks in anticipation, Dave Higgs
cannot see where to locate the php Header function
Moderator: General Moderators
Re: cannot see where to locate the php Header function
Not so. Place the header call above all output, but wrap it in a conditional so it's only triggered if the form has been submitted.higgsy142 wrote:On my Page1 I must have the HTML ahead of the Header call else how would I be able to display the form?
Code: Select all
<?php
if (!empty($_POST)) {
// do login check here
header('Location: foo');
exit;
}
<html>
... etcRe: cannot see where to locate the php Header function
Celauran: Thank you so much for your reply, I eventually implemented your structure, putting the PHP ahead of the HTML and got it working locally on my XAMPP server. However, I have just uploaded it to my host (1and1.co.uk) and have got the dreaded “cannot redirect” error again!
The page is at http://dave.higgsy.com/dvds/05_login_url.php. The logon page needs the User Id and the Password fields filled in, both are ‘dh’
The only changes I made to this online version were the four SQL variables which name the 1and1 host, database, user and its password (lines 23 thru 26).
I have a variable called $msg_on This is currently set to “yes” to allow us to see the database calls are all working because 1 row is returned. Hopefully Celauran or any other helpful guru can see what is wrong…many thanks, Dave
The page is at http://dave.higgsy.com/dvds/05_login_url.php. The logon page needs the User Id and the Password fields filled in, both are ‘dh’
The only changes I made to this online version were the four SQL variables which name the 1and1 host, database, user and its password (lines 23 thru 26).
I have a variable called $msg_on This is currently set to “yes” to allow us to see the database calls are all working because 1 row is returned. Hopefully Celauran or any other helpful guru can see what is wrong…many thanks, Dave
Re: cannot see where to locate the php Header function
Here is the php which all appears before the html tags, as suggested by celauran
Code: Select all
<?php
session_start();
error_reporting(E_ALL); ini_set('display_errors', 'On');
$msg_on = "yes";
function write_log($msg)
{
echo "<br /><h3>write_log says: </h3>";
echo $msg;
echo "<br />";
error_log("write_log says: ");
error_log($msg);
}
if (!empty($_POST)) {
/* $host="localhost";
$dbname="dbase_users_for_site_dave";
$user="root";
$passwd=""; /* ASUS has no pwd for mysql, it is kirstie1 on VAIO */
$host="db521201687.db.1and1.com";
$dbname="db521201687";
$user="dbo521201687";
$passwd="kirstie1"; /* ASUS has no pwd for mysql, it is kirstie1 on VAIO */
$cxn = mysqli_connect($host,$user,$passwd) or die ("05_login_url.php issues: HIGGS001E1 couldn't connect to server");
if (mysqli_connect_errno($cxn))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
if ($msg_on == "yes")
{
write_log("connected ok");
}
}
$query = "SELECT * FROM table_of_users WHERE user_name='$_POST[form_username]' AND the_password='$_POST[form_password]'";
if ($msg_on == "yes")
{
write_log("here is the query i will use");
echo "<br />";
echo $query;
echo "-------------3 ---------------";
}
$db = mysqli_select_db($cxn,$dbname);
$result = mysqli_query($cxn,$query);
if ( false===$result ) {
if ($msg_on == "yes")
{
printf("query returned FALSE: %s\n", mysqli_error($cxn));
}
}
else {
if ($msg_on == "yes")
{
echo 'query returned TRUE.';
}
$n_row = mysqli_num_rows($result);
if ($msg_on == "yes")
{
printf("Result set has %d rows.\n",$n_row);
}
echo "<br />";
if($n_row > 0) // if login successful
{
setcookie("higgscookie","success");
header("Location: 10_search_entry_form.php");
} // end if submitted
else
{
$message = "<p style='color: red; margin-bottom: 0; font-weight: bold'>
User ID and Password not found.</p>";
}
}
}
?>
Last edited by Celauran on Sun Mar 30, 2014 6:24 am, edited 1 time in total.
Reason: Please wrap code in syntax tags to keep things legible
Reason: Please wrap code in syntax tags to keep things legible
Re: cannot see where to locate the php Header function
You have to send the header before ANY output, that includes your log messages not just formatted HTML. Look into how a web server works.write_log says:
connected ok
write_log says:
here is the query i will use
SELECT * FROM table_of_users WHERE user_name='dh' AND the_password='dh'-------------3 ---------------query returned TRUE.Result set has 1 rows.
Re: cannot see where to locate the php Header function
to johnnycake and celauran: Many thanks chaps (or chapesses!?) Following your advice I found an echo "<(br/)>" which I had been using during my efforts to fix it. That must have been doing the forbidden output. It now works fine. As an aside, after I had posted the php code here, I realised I should have put it between PHP tags so thanks celauran for doing that. Standby for more dumb questions! regards, Dave