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 :)

 Answers

1
<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
3

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:

<?php

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'];
    }
}

var_dump($config);

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.

EDIT

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
 
1

To iterate over a multidimensional array, you can use RecursiveArrayIterator

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

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

Output:

John:
status => Wait
Jennifer:
status => Active
James:
status => Active
age => 56
count => 10
progress => 0.0029857
bad => 0

run on codepad

Thursday, September 29, 2022
 
sargas
 
2

If you don't want to use javascript, you can handle it via php. Take a look at this lib: http://code.google.com/p/php-mobile-detect/. And then you could do something like:

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

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

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('http://www.myphpserver.com/script.php?row_id=' + 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 :