本格的に取り組みたい場合は、HTML ページに .js ファイルを要求させ、セッション ID またはその他の人物を示すインジケーターと組み合わせて、.js 呼び出しを PHP 呼び出しとして操作し、セッションが必要とするものに基づいて JS を作成し、それを .js ファイルタイプとしてブラウザーに出力します。
しかし、それは大変な作業です。
においが少ないものが必要な場合は、ファイルの末尾にある JSON 文字列を PHP にダンプさせます。
var cfg_string = "{\"username\":\"Norguard\", \"new_messages\":[......]}"; // client
$cfg_obj = array(); // whole lot o'PHP
$json_encoded_cfg = json_encode($cfg_obj);
echo "var cfg_string = {$json_encoded_cfg};" //server-side
そして、安全性を高めるためにクライアントで解析します...
...または、テンプレートで完全にマップを作成します。
$cfg_string = "var dataMap = {";
foreach ($cfg_obj as $key => $val) {
// print key:val all pretty-like,
// handle commas (ie: no trailing comma at the end), indent with tabs or spaces
// if you want, count the number of items so that the object closes ({})
// without any newline operator, if there are no config settings
}
echo $cfg_string;
これらはどちらもクリーンで目立たず、すべてを分離しています。構成データ/テキストは、初期化/読み込みコードがどのようなものであれ、すぐ上に移動し、その初期化ロジックにパラメーターとして渡すことができます。