我怎样才能回复JS重定向? [英] How can I echo JS redirection?

查看:77
本文介绍了我怎样才能回复JS重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想回应一个javascript移动设备重定向到移动网站。

但是我必须给出一个带有重定向链接的'id'。这是我现在的代码:

I want to echo a javascript mobile device redirection to the mobile site.
But I have to give an 'id' with the redirect link. This is my code as of now:

<?
  if (screen.width <= 800) {
    window.location("mplayer.php?id=" . $_GET['id'] . "");
  }
?>

但它给了我这个错误


注意:使用未定义的常量屏幕 - 在第16行的/home/jterweele/public_html/pack-simulator/16/player.php中假定为'screen'

Notice: Use of undefined constant screen - assumed 'screen' in /home/jterweele/public_html/pack-simulator/16/player.php on line 16

注意:使用未定义的常量宽度 - 在第16行的/home/jterweele/public_html/pack-simulator/16/player.php中假定为'width'

Notice: Use of undefined constant width - assumed 'width' in /home/jterweele/public_html/pack-simulator/16/player.php on line 16

注意:使用未定义的常量窗口 - 在第17行的/home/jterweele/public_html/pack-simulator/16/player.php中假定为'window'

Notice: Use of undefined constant window - assumed 'window' in /home/jterweele/public_html/pack-simulator/16/player.php on line 17

致命错误:在第17行的/home/jterweele/public_html/pack-simulator/16/player.php中调用未定义的函数位置()

Fatal error: Call to undefined function location() in /home/jterweele/public_html/pack-simulator/16/player.php on line 17

希望有人可以帮助我。
谢谢!

Hope someone can help me. Thanks!

推荐答案

JS和PHP是两种不同的动物,它们不会像你使用它们那样混合在一起目前,这解释了你得到的语法错误,因为你宣称PHP认为是常量。

JS and PHP are two different animals and they do not mix together the way you are using those presently, which explains the syntax errors you are getting, because you are declaring what PHP thinks are constants.

  • http://php.net/manual/en/function.constant.php

你需要做的是分开那些。

What you need to do is to seperate those.

首先,JS / HTML:

First, the JS/HTML:

<!DOCTYPE html>

<head>
   <title></title>
</head>

<body>
    <script type="text/javascript">

    <!--
    if (screen.width <= 800) {
       window.location = "redirection_page.php?set=true";
    }

    else {
       document.write ("Show them a message, or do something else.");
    }
    //-->
    </script>

</body>
</html>

然后PHP:(redirection_page.php)

Then the PHP: (redirection_page.php)

<?php

if(isset($_GET['set']) && $_GET['set'] == true ){

header("Location: http://www.example.com/");

exit;

}






所以,在你的情况下会读到这样的东西:


So, in your case that would read something like this:

NB:(你需要对GET数组的内容做一些修改be)。

N.B.: (You will need to do some modifications as to what the GET array will be).

<!DOCTYPE html>

<head>
   <title></title>
</head>

<body>

<?php 

$_GET['id'] = 2;

?>

    <script type="text/javascript">

    <!--
    if (screen.width <= 800) {
       window.location = "mplayer.php?id=<?php echo $_GET['id'] ?>";
    }

    else {

       document.write ("Show them a message, or do something else.");

    }

    //-->
    </script>

</body>

</html>

PHP:(mplayer.php)

PHP: (mplayer.php)

<?php

if(isset($_GET['id']) && $_GET['id'] == 2 ){

   echo $id = $_GET['id'];

}






确保你没有在标题之前输出(这意味着PHP之上没有HTML,之前的空格<?php 标签,一个cookie等等。如果这样做'为您工作,那么您的系统可能不会设置为捕获/显示错误/通知/警告,因此启用错误报告。


Also make sure that you are not outputting before header (this means no HTML above the PHP, a space before the <?php tag, a cookie, etc. If this doesn't work for you, then your system may not be set to catch/display errors/notices/warnings, so enable error reporting.

如果您确实看到标题已发送通知,阅读Stack上的以下内容:

If you do see a headers sent notice, read the following on Stack:

  • How to fix "Headers already sent" error in PHP

添加 错误报告 到您的文件顶部,这有助于查找错误。

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

旁注:只应在暂存时进行显示错误,而不是生产。

Sidenote: Displaying errors should only be done in staging, and never production.

这篇关于我怎样才能回复JS重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆