Viewed   74 times

I have a JavaScript file where I would like to include some php code. The problem is that I have a few defines on PHP that I would like to use on JS as well.

Is there any way of including a .js file in HTML allowing the server to first interpret it (before downloading to the client) using php?

Thanks :)


<script src="/path/to/my/file.php"></script>

In file.php you'll also want to output the correct header, before outputting anything you should have the following:

header("Content-Type: application/javascript");

EDIT: As @Tony_A pointed out, it should be application/javascript. I don't think it mattered as much when I wrote this post in 2010 :)

Wednesday, September 28, 2022

No, there is no special function to parse httpd.conf, but a parser should be easy to write. For example, if you're only interested in the key-value settings, like ServerRoot /var/www, then this will do the trick:


define('HTTPD_CONF', '/tmp/httpd.conf');

$lines = file(HTTPD_CONF);
$config = array();

foreach ($lines as $l) {
    preg_match("/^(?P<key>w+)s+(?P<value>.*)/", $l, $matches);
    if (isset($matches['key'])) {
        $config[$matches['key']] = $matches['value'];


If you want to parse the <Directory ...> and other blocks, it'll take you a few more lines of code, but it shouldn't be too taxing. It really depends on what you want to do. You can get a lot of info from $_SERVER and getenv(), so you might not need to parse a config file.


In response to your update, for editing httpd.conf, you'll need to run your script with superuser privileges and cause httpd.conf to be reloaded (e.g., system("apachectl graceful");).

Tuesday, October 25, 2022

To iterate over a multidimensional array, you can use RecursiveArrayIterator

$jsonIterator = new RecursiveIteratorIterator(
    new RecursiveArrayIterator(json_decode($json, TRUE)),

foreach ($jsonIterator as $key => $val) {
    if(is_array($val)) {
        echo "$key:n";
    } else {
        echo "$key => $valn";


status => Wait
status => Active
status => Active
age => 56
count => 10
progress => 0.0029857
bad => 0

run on codepad

Thursday, September 29, 2022

If you don't want to use javascript, you can handle it via php. Take a look at this lib: And then you could do something like:

include 'Mobile_Detect.php';
$detect = new Mobile_Detect();

if ($detect->isMobile()) {
    header('Location: yourpage.php');
Friday, October 21, 2022

Cookies are not the way to transfer variables between client and server. you should append key/variables pairs to your request URL using either a get (querystring) or post method.

jQuery ajax example;

$.get('' + NewCookieValue);
Monday, October 31, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :