FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP script problem





riyadh
i have a script for sohwing daily quotes on my webpage but something is wrong in the coding and an error message shows up. but the quote still shows up under the error message. help someone!!!!



this is the code for the script:

<?php
$numDB = 1;

$dbFileName = array("quotes.db");

$quoteType = 2;
/*
The variable $quoteType determines the type of quote generation.
quoteType = 0 displays a new random quote each time the page is loaded.
quoteType = 1 displays a new random quote every 60 minutes.
quoteType = 2 displays a new random quote every 24 hours.
quoteType = 3 displays a new random quote once every 7 days.
Settings 1, 2 & 3 are handled by storing a cookie. Use the included "del_cookie.php" script to delete the cookie while you are testing the script.
*/
if ($numDB == 1){
$filename = $dbFileName[0];
}else{
$ranDB = rand(1, $numDB);
$filename = $dbFileName[$ranDB-1];
}
/*
This conditional Statement if ($numDB == 1){.... is picking the database from which to pull the quotes. If the variable $numDB is set to 1 it pulls the first db in the array. You will notice that $dbFileName is set to 0. This is because most languages consider 0 to be the first number when counting.
*/
$lines = file($filename);
$numQuotes = count ($lines);

if ($quoteType == 0){
$ranNum = rand(1, $numQuotes);
} elseif ($quoteType == 1){
if (isset($_COOKIE['quoteNum'])) {
$ranNum = $_COOKIE['quoteNum'];
} else {
$ranNum = rand(1, $numQuotes);
setcookie("quoteNum", $ranNum, time()+60*60);// One Hour
}
}elseif ($quoteType == 2){
if (isset($_COOKIE['quoteNum'])) {
$ranNum = $_COOKIE['quoteNum'];
} else {
$ranNum = rand(1, $numQuotes);
setcookie("quoteNum", $ranNum, time()+60*60*24); //Twenty-four Hours
}
}elseif ($quoteType == 3){
if (isset($_COOKIE['quoteNum'])) {
$ranNum = $_COOKIE['quoteNum'];
} else {
$ranNum = rand(1, $numQuotes);
setcookie("quoteNum", $ranNum, time()+60*60*24*7); // Seven Days
}
}
/*
This conditional Statement if ($quoteType == 0){.... is determining the interval at which you want the quotes displayed. This feature uses the Set Cookie function. Which places a cookie in the users browser.
*/
$line = $lines[$ranNum-1];
$element = explode("|",$line);
$theQuote = $element[0];
$theRef = $element[1];
include ('quote_func.php'); // this references the HTML that will be output to the browser.
// $output = outputLine($theQuote, $theRef);
$output = outputTable($theQuote, $theRef);
echo $output;
?>


this is the error message i get:
Warning: Cannot modify header information - headers already sent by (output started at /home/./domains/dreamer.frih.net/public_html/index.php:7) in /home/./domains/dreamer.frih.net/public_html/quotes.php on line 41[/b]
ncwdavid
I am not very good at php but i think it means there has been output on the screen like echo "something" before the header has been sent and the header need to be the first output.

P.S you are supposed to post your code like this:
Code:

<?php
$numDB = 1;

$dbFileName = array("quotes.db");

$quoteType = 2;
/*
The variable $quoteType determines the type of quote generation.
quoteType = 0 displays a new random quote each time the page is loaded.
quoteType = 1 displays a new random quote every 60 minutes.
quoteType = 2 displays a new random quote every 24 hours.
quoteType = 3 displays a new random quote once every 7 days.
Settings 1, 2 & 3 are handled by storing a cookie. Use the included "del_cookie.php" script to delete the cookie while you are testing the script.
*/
if ($numDB == 1){
$filename = $dbFileName[0];
}else{
$ranDB = rand(1, $numDB);
$filename = $dbFileName[$ranDB-1];
}
/*
This conditional Statement if ($numDB == 1){.... is picking the database from which to pull the quotes. If the variable $numDB is set to 1 it pulls the first db in the array. You will notice that $dbFileName is set to 0. This is because most languages consider 0 to be the first number when counting.
*/
$lines = file($filename);
$numQuotes = count ($lines);

if ($quoteType == 0){
$ranNum = rand(1, $numQuotes);
} elseif ($quoteType == 1){
if (isset($_COOKIE['quoteNum'])) {
$ranNum = $_COOKIE['quoteNum'];
} else {
$ranNum = rand(1, $numQuotes);
setcookie("quoteNum", $ranNum, time()+60*60);// One Hour
}
}elseif ($quoteType == 2){
if (isset($_COOKIE['quoteNum'])) {
$ranNum = $_COOKIE['quoteNum'];
} else {
$ranNum = rand(1, $numQuotes);
setcookie("quoteNum", $ranNum, time()+60*60*24); //Twenty-four Hours
}
}elseif ($quoteType == 3){
if (isset($_COOKIE['quoteNum'])) {
$ranNum = $_COOKIE['quoteNum'];
} else {
$ranNum = rand(1, $numQuotes);
setcookie("quoteNum", $ranNum, time()+60*60*24*7); // Seven Days
}
}
/*
This conditional Statement if ($quoteType == 0){.... is determining the interval at which you want the quotes displayed. This feature uses the Set Cookie function. Which places a cookie in the users browser.
*/
$line = $lines[$ranNum-1];
$element = explode("|",$line);
$theQuote = $element[0];
$theRef = $element[1];
include ('quote_func.php'); // this references the HTML that will be output to the browser.
// $output = outputLine($theQuote, $theRef);
$output = outputTable($theQuote, $theRef);
echo $output;
?>
hexkid
riyadh wrote:
Warning: Cannot modify header information - headers already sent by (output started at /home/./domains/dreamer.frih.net/public_html/index.php:7) in /home/./domains/dreamer.frih.net/public_html/quotes.php on line 41[/b]
You problem starts in index.php. Line 7 of index.php sent some "normal html" to the client and, because of that, the statement on line 41 of quotes.php (one of the setcookie()'s??? [setcookie(...); is just about the same as header('Set-Cookie: ...');]) could not be executed. All headers of a response must be sent before any "normal html".

Solutions:
a) delete line 7 from index.php (just kidding Smile )
b) rework your scripts so that all headers get sent before "normal html".
w) yes! w), not c) use ob_start() and related functions.
riyadh
it would be nice if u can tell a tad bit more on how i can fix this script. i'm really confused cos line 7 in index.php is the <style> tag.
hexkid
riyadh wrote:
it would be nice if u can tell a tad bit more on how i can fix this script. i'm really confused cos line 7 in index.php is the <style> tag.
You can't have "normal html" sent to the client before headers (setcookie() and session_start() included).

This is wrong
Code:
<!DOCTYPE ...>
<?php header('Content-Type: text/html'); ?>

The "<!DOCTYPE ...>" is "normal html" and must be sent after all headers
Code:
<?php header('Content-Type: text/html'); ?>
<!DOCTYPE ...>


If you have a mixed mess of output, headers and include files; it may not be easy to change them all to comply with the HTTP requirements.
Code:
<?php header('Content-Type: text/html'); ?>
<!DOCTYPE ...>
<head><title>example</title></head><body>
<?php include 'foobar.php'; /* foobar.php sets a cookie */ ?>
</body></html>


What you have to do in this situation is to move the setcookie() calls (and header() and session_start()) away from foobar.php and put them in the "main" file
Code:
<?php header('Content-Type: text/html'); ?>
<?php setcookie('...'); /* moved from foobar.php */ ?>
<!DOCTYPE ...>
<head><title>example</title></head><body>
<?php include 'foobar.php'; /* foobar.php does not set cookies */ ?>
</body></html>


If you don't mind "killing flies with cannons" have a look at ob_start() and related functions.
riyadh
i tried doing what u said. writing "<?php header" before the the normal html in index.php but the error is still there. it would help if u pointed out to me a code change in my original code provided at the top of the page. Very Happy
hexkid
riyadh wrote:
i tried doing what u said. writing "<?php header" before the the normal html in index.php but the error is still there. it would help if u pointed out to me a code change in my original code provided at the top of the page. Very Happy


Your original code provided at the top of the page requires that no "normal HTML" have been sent before control reaches its first line.

Either change index.php so that it doesn't output "normal HTML" or, if you really only want to change the original code provided at the top of the page, delete the setcookie() lines from it -- of course that if you do so, no cookies will be sent to the client.
rfwrangler
If you are using this file as an include or required file in your index page then the file has to be included before any of the html is outputted to the browser. The headers set up the sessions that are used on the page and have to be set before the browser starts to recieve any information. If these headers are not set first it confuses the browser and the session is not set right.

Your index.php file should look something like the following:
Code:

<?php
    include "path/to/phpinclude.php";
?>
<html>
<DOCTYPE.............>
<head>
<head content.......>
</head>
<body>
     Body content
</body>
</html>




This will set the session in the right area and should clear up your problem.
riyadh
i see what u mean but the script can't be in the headers cos it won't output anything on the page. i tried to make a separate file for the cookies and include it in the index before normal html but there was still an error.
the zephyrus
What you can do is hold all your page's stuff inside a string variable. Then output the headers and the html tags wrapped around the body. Then echo() the string you made earlier.
Related topics
Phoenix free PHP scripts
error in my PHP script
how can i make a php script for enter username and password?
Inserting data to MYSQL with a PHP script
Running a "Hello World" php script
[PHP] Running Cron Jobs Via PHP Script
Simple PHP script not working
PHP Script- How can we install it
I need a VERY simple php script
PHP Script needed
Logging systeem php script
Preventing bad PHP script calls
array check box on php script using $_POST
php script not loading
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.