473,396 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3031
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: josh dismukes | last post by:
/// here is the code i'm getting a parse error on the last line of the code which /// is </html> any help will be much appreciated. <?php session_start ();
9
by: Philip TAYLOR | last post by:
Configuring a new instance of IIS, I noticed that it allows an HTML-formatted document trailer to be appended to every document served. Unfortunately, on checking its behaviour, I find that it...
10
by: Kathy Burke | last post by:
Hi, in trying to discover why my RegisterStartUpScript wouldn't work (I do NOT see it in the HTML source), I looked at the HTML source of a page where I do an XslTransform. First, I get the...
2
by: taras.di | last post by:
Hi everyone, Is it possible to place javascript outside of <html> tags? I'm trying it on mozilla atm, and it seems to be working, but I was more worried about the older browsers. Cheers ...
4
by: Mark G. | last post by:
Hello. I am attempting to write a "scraper" to download information from a commercial web site. Oddly enough, they don't want to make this easy for me! Their pages include plenty of Javascript,...
1
by: yawnmoth | last post by:
I'm trying to mess around with PHP5's DOM functions and have run into something that confuses me: <?php $dom = new DOMDocument(); $dom->loadHTML('<html></html>'); echo...
1
by: John | last post by:
Hi var poster="<html><head..... etc .... </html>"; var animal='dog'; The string contains images and text that changes. Originally I wanted to do something like print "<a href=" +...
3
by: Silmaril | last post by:
hi everyone, i have a question about place of metatags. i've started to seo for active webpage of our client(which is not coded by me) ,but website is very bad coded then i'm a lazy programmer...
3
by: Arodicus | last post by:
This is bugging me: how do I reference the topmost node (HTML) within a document? I'd like to set a class on it, which specifies various browser/os/versions so that several...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.