英文:
How do I insert a php variable inside javascript inside PHP
问题
我在本地网络服务器上使用了以下代码,没有任何问题:
```php
function redirect($url) {
header('Location: '.$url);
exit();
}
但在我的托管服务器上出现了“输出已发送”的错误。我设计了一个JavaScript 解决方法,如下所示:
function redirect_to($new_location)
{
$redirects = [
"<script>window.location.replace(\"login.php\");</script>",
"<script>window.location.replace(\"select_set.php\");</script>",
"<script>window.location.replace(\"run_order.php\");</script>",
"<script>window.location.replace(\"display_setlist.php\");</script>",
"<script>window.location.replace(\"manage_admins.php\");</script>",
"<script>window.location.replace(\"clients.php\");</script>",
"<script>window.location.replace(\"manage_songs.php\");</script>",
"<script>window.location.replace(\"admin.php\");</script>"
];
for ($i=0;$i<count($redirects);$i++) {
if (strpos($redirects[$i], $new_location)) {
echo $redirects[$i];
}
}
}
这种方法不够优雅,每次重定向都需要编写一个单独的脚本。
我的问题是:是否有办法将 PHP 变量 $new_location
插入到 JavaScript 字符串中?是否可行?
我学会了如何在 PHP 和 JavaScript 中相互插入代码。我尝试过以各种方式结合这两种插入方法,但没有成功。
<details>
<summary>英文:</summary>
I used:
`function redirect($url) {header('Location: '.$url);
exit();
}`
on my local network server without any problems but it failed with 'output already sent' errors on my hosting server. I devised a javascript work-around that works as shown in the code below.
function redirect_to($new_location)
{
$redirects = [
"<script>window.location.replace(\"login.php\");</script>",
"<script>window.location.replace(\"select_set.php\");</script>",
"<script>window.location.replace(\"run_order.php\");</script>",
"<script>window.location.replace(\"display_setlist.php\");</script>",
"<script>window.location.replace(\"manage_admins.php\");</script>",
"<script>window.location.replace(\"clients.php\");</script>",
"<script>window.location.replace(\"manage_songs.php\");</script>",
"<script>window.location.replace(\"admin.php\");</script>"
];
for ($i=0;$i<count($redirects);$i++) {
if (strpos($redirects[$i], $new_location)) {
echo $redirects[$i];
}
}
}
It's not elegant and requires a separate script to be written for each redirect.
My question: Is there a way to insert the PHP variable '$new_location' into the javascript string. Can it be done?
I learned how to insert javascript into php and vice versa. I've tried combining the two insertion methods in various ways without success.
</details>
# 答案1
**得分**: -1
你真的应该使用 .htaccess 文件
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=301]
查找和头部重定向应该可以工作,你应该调查为什么它不起作用。
你很可能插入了空格,如
?>
<?php
一旦弄清楚托管情况,你可以使用以下版本。他们的帮助台应该能够提供帮助。
```php
function redirect_to($new_location) {
$redirects = [
'login' => 'login.php',
'select_set' => 'select_set.php',
'run_order' => 'run_order.php',
'display_setlist' => 'display_setlist.php',
'manage_admins' => 'manage_admins.php',
'clients' => 'clients.php',
'manage_songs' => 'manage_songs.php',
'admin' => 'admin.php'
];
if(array_key_exists($new_location, $redirects)) {
header('Location: ' . $redirects[$new_location]);
exit;
}
}
英文:
You really should use .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L,R=301]
A lookup and header redirect should work and you should investigate why it does not work.
You likely inserted whitespace like
?>
<?php
Here is a version you can use once you figured out what is going on on the hosting. Their helpdesk should be able to help.
function redirect_to($new_location) {
$redirects = [
'login' => 'login.php',
'select_set' => 'select_set.php',
'run_order' => 'run_order.php',
'display_setlist' => 'display_setlist.php',
'manage_admins' => 'manage_admins.php',
'clients' => 'clients.php',
'manage_songs' => 'manage_songs.php',
'admin' => 'admin.php'
];
if(array_key_exists($new_location, $redirects)) {
header('Location: ' . $redirects[$new_location]);
exit;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论