getallheaders() is disabled, why?

Status
Not open for further replies.

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
I am developing a mobile site.For this I want to know request headers from visting browsers and want to log them.So I can design a mechanism where I can detect handset browsers name(User agent) and serve content according to this.
As you might know that there are plenty of mobile browsers, along with some transcoders(like "http://m.google.com/gwt/n" and "Opera mini").
Normal phone browsers user-agent can be accessed using $_SERVER['HTTP_USER_AGENT'], but transcoders sends their own headers for example, if I want to know original user-agent of opera mini host, then I need to access different header $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'].and something similar for IP address.
I searched through internet so I got these headers.But there are plenty of mobile networks and 3rd party browsers who are doing this.So it will be good, if I store these headers this server temperoraily and study them on so I can design best detection system.I am using WURFL framework.But it is not good at detecting transcoders(means:- who modify original headers,sometimes mobile networks do this too,like vodaone UK).
So I wrote this to get header info
PHP:
<?php
$file_name="welcome.html";
foreach (apache_request_headers() as $name => $value) 
{
$body_content=$value;
$fp=fopen($file_name,"a");
fwrite ($fp,$body_content);          // entering data to the file
fclose ($fp);                                // closing the file pointer
chmod($file_name,0777);
}
?>
But neither apache_request_headers() nor getallheaders() is working on my free account.I am new to php programming(only 1 month experience).So I want to know what is the reason behind these functions are unfunctionalty and what could be the solution for my problem.
 
Last edited:

galaxyAbstractor

Community Advocate
Community Support
Messages
5,508
Reaction score
35
Points
48
getallheaders() only works when PHP is installed as an Apache module on the servers, which in this case it isn't. So best bet is to try to find an alternate way of getting them.

Could you see if this works:

PHP:
<?php
function emu_getallheaders() {
   foreach($_SERVER as $name => $value)
       if(substr($name, 0, 5) == 'HTTP_')
           $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
   return $headers;
}
?>

Call emu_getallheaders() instead of getallheaders()
 
Last edited:

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
Thanks for info but the output I am getting is just information inside headers.But I want information plus headers name.Because headers are case sensitive.And greedy mobile operators and proxy services changes them.So I want to know what is the name of headers they are sending and its value.
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
Hi oceanwap,

It gives the header name too.

Check it.


PHP:
function emu_getallheaders() {
   foreach($_SERVER as $name => $value)
       if(substr($name, 0, 5) == 'HTTP_')
           $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
   return $headers;
}

$headers = emu_getallheaders();

foreach ($headers as $header => $value) {
    echo "$header: $value <br />\n";
}
 

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
Thanks a lot, It is working perfectly now.Now the only work left is store each $header: $value <br />\n to a file so that I can save this information.And I did this like this
PHP:
<?php
function emu_getallheaders() {
   foreach($_SERVER as $name => $value)
       if(substr($name, 0, 5) == 'HTTP_')
           $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
   return $headers;
}
$headers = emu_getallheaders();
$file_name="welcome.html";
foreach (emu_getallheaders() as $name => $value) {
$body_content="$name: $value";
$add_line_break='<br />';
$fp=fopen($file_name,"a");
fwrite ($fp,$body_content.$add_line_break);          // entering data to the file
fclose ($fp);                                // closing the file pointer
chmod($file_name,0777);
}
?>
 
Last edited:

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
Thanks a lot, It is working perfectly now.Now the only work left is store each $header: $value <br />\n to a file so that I can save this information.And I did this like this

Instead of opening the file for every header field, Open the file outside the for loop and close the file after the for loop.

PHP:
<?php
function emu_getallheaders() {
   foreach($_SERVER as $name => $value)
       if(substr($name, 0, 5) == 'HTTP_')
           $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
   return $headers;
}
$headers = emu_getallheaders();
$file_name="welcome.html";
$fp=fopen($file_name,"a");
foreach (emu_getallheaders() as $name => $value) {
$body_content="$name: $value";
$add_line_break='<br />';
fwrite ($fp,$body_content.$add_line_break);          // entering data to the file
}
fclose ($fp);                                // closing the file pointer
chmod($file_name,0755);
?>

But It is recommended to store the information in MySQL database. And when ever required display from the database.
 

oceanwap

New Member
Messages
24
Reaction score
0
Points
0
Again thanks for your suggestion.Opening and closing file in a loop is not a good idea,But free account has limitation of 3 databases.Suppose I have a joomla database,so can I use that to create tables for this header detection system.or I should create new one.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
Again thanks for your suggestion.Opening and closing file in a loop is not a good idea,But free account has limitation of 3 databases.Suppose I have a joomla database,so can I use that to create tables for this header detection system.or I should create new one.
You can put an almost unlimited number of tables inside a single database. Simply make sure that there is no name conflict and you can easily use the same database to store the headers.

- xav0989
 
Status
Not open for further replies.
Top