468,785 Members | 1,705 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,785 developers. It's quick & easy.

calling external php file before <html> tag

Ben
hi all,

just wondering if it is possible to call a function that resides on
external php file before any <html> tag?? i have a code that sets a
cookie which needs to be called before <html> tag. If I use that code
on the same file, it works fine. But I'd like to use it on a separate
php file so that it'd be easier to modify in the future because that
same function can be called from other files too... When I include the
external php file in my main file, it gives me warning saying
something like the cookie is set by the external file.... Is there a
way to do it properly?

Thanx
Ben
Jul 17 '05 #1
5 2825
include("file_path/file_name.php");
function_name(arg1,arg2,[...],argX);

Basically, you include the file, then call the function contained in the
file.

-Wes

"Ben" <cr*********@yahoo.com> wrote in message
news:d9**************************@posting.google.c om...
hi all,

just wondering if it is possible to call a function that resides on
external php file before any <html> tag?? i have a code that sets a
cookie which needs to be called before <html> tag. If I use that code
on the same file, it works fine. But I'd like to use it on a separate
php file so that it'd be easier to modify in the future because that
same function can be called from other files too... When I include the
external php file in my main file, it gives me warning saying
something like the cookie is set by the external file.... Is there a
way to do it properly?

Thanx
Ben

Jul 17 '05 #2
Shit. Wasn't thinking all the way again. You can shoot me if you want! A
warning isn't necessicarily saying that you've done something horribly
wrong. It's just informing you of something that could be potentially
hazordous. There is a setting in PHP.INI that allows you to stop the
warnings.

Please feel free to correct me as needed.

-Wes

"Ben" <cr*********@yahoo.com> wrote in message
news:d9**************************@posting.google.c om...
hi all,

just wondering if it is possible to call a function that resides on
external php file before any <html> tag?? i have a code that sets a
cookie which needs to be called before <html> tag. If I use that code
on the same file, it works fine. But I'd like to use it on a separate
php file so that it'd be easier to modify in the future because that
same function can be called from other files too... When I include the
external php file in my main file, it gives me warning saying
something like the cookie is set by the external file.... Is there a
way to do it properly?

Thanx
Ben

Jul 17 '05 #3
Ben
"Wes Spikes" <Mo******@NOSPAMverizon.net> wrote in message news:<3GdYc.640$O85.477@trnddc05>...
include("file_path/file_name.php");
function_name(arg1,arg2,[...],argX);

Basically, you include the file, then call the function contained in the
file.

Thanx Wes!

That's exactly what I did but it doesn't work...
My external php file is below (this sets a cookie):
<?php
function counter() {
$cookie_val = @$_COOKIE["user_ip"];
$counterfile = "counter";
$line = @file($conterfile);
if(!$cookie_val) {
setcookie("user_ip", "$_SERVER[REMOTE_ADDR]", time()+36000);
if ($line[0] == NULL) {
$line[0] = 0;
}
$line[0]++;
$cf = @fopen($counterfile, "w+");
fputs($cf, "$line[0]");
fclose($cf);
}
elseif ($cookie_val != "$_SERVER[REMOTE_ADDR]") {
$line[0]++;
$cf = @fopen($counterfile, "w+");
fputs($cf, "$line[0]");
fclose($cf);
}
}
?>

My main (calling) php file is like this:
<?php
include 'phpfunc.php';
counter();
?>
<html>
<head></head>
<body></body>
</html>

I get the following warning:
Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\phpfunc.php:26) in
D:\Apache Group\Apache2\htdocs\phpfunc.php on line 8

I also uncommented "include_path" in php.ini and added the path to my
localhost web server which is "D:\Apache Group\Apache2\htdocs\" but
nothing changed...

Hope this shows my problem more clearly..

Thanx!
Ben
Jul 17 '05 #4
duz
cr*********@yahoo.com (Ben) wrote in message news:<d9**************************@posting.google. com>...
<snip>

I get the following warning:
Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\phpfunc.php:26) in
D:\Apache Group\Apache2\htdocs\phpfunc.php on line 8


Make the very first line of your main file ob_start() and after you're
done with setting the cookie do a ob_end_flush(). The first command
will cause PHP to buffer the output until the second command is
executed. This also means you don't need to put your cookie function
before the <html> but it is a good idea to execute it as soon as you
can so you don't have to worry about buffering too much output.

Ben
www.dq5studios.com
Jul 17 '05 #5
Ben
dq********@gmail.com (duz) wrote in message news:<79**************************@posting.google. com>...
cr*********@yahoo.com (Ben) wrote in message news:<d9**************************@posting.google. com>...
<snip>

I get the following warning:
Warning: Cannot modify header information - headers already sent by
(output started at D:\Apache Group\Apache2\htdocs\phpfunc.php:26) in
D:\Apache Group\Apache2\htdocs\phpfunc.php on line 8


Make the very first line of your main file ob_start() and after you're
done with setting the cookie do a ob_end_flush(). The first command
will cause PHP to buffer the output until the second command is
executed. This also means you don't need to put your cookie function
before the <html> but it is a good idea to execute it as soon as you
can so you don't have to worry about buffering too much output.


thanx duz,
it's working now...
i did like this in my main file:
<?php
ob_start();
include 'phpfunc.php';
counter();
ob_end_flush()
?>
<html><body></body></html>

Ben
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by josh dismukes | last post: by
2 posts views Thread by taras.di | last post: by
4 posts views Thread by Mark G. | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.