基于GET变量的PHP重定向页面 [英] PHP redirection page based on GET variables

查看:85
本文介绍了基于GET变量的PHP重定向页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP新手,所以请在这个初级问题上忍受我。

我想创建一个脚本,根据GET变量将用户重定向到各种地址。例如, redirection.php?id = youtube 应将其重定向到www.youtube.com, redirection.php?id = twitter 应将其重定向到www.twitter.com , 等等。



以下是我的代码:

 <!DOCTYPE html> 
< html>
< head>
< title>请稍候...< / title>
< / head>
< body>

<?php
//将用户导向互联网上的不同位置
print_r($ _ GET);
$ b $ if($ _ GET ['id'] ==='youtube'){
header('Location:http://www.youtube.com/');
die()
}
if($ _ GET ['id'] ==='twitter'){
header('Location:http://www.twitter。 com /');
die()
}
if($ _ GET ['id'] ==='reddit'){
header('Location:http://www.reddit。 com /');
die()
}
?>

< / body>
< / html>

到目前为止,PHP文件根本没有反应,我该如何改变以解决这个问题?



再次,对于初级问题抱歉,但这实际上是我的第一个PHP脚本,我不熟悉一些使Google搜索正确代码非常困难的术语。

解决方案

当比较PHP中的值时,您可以使用==运算符或===运算符。 2的区别是什么?那么,这很简单。 ==运算符只是检查左右值是否相等。但是,===运算符(注意额外的=)实际上检查左右值是否相等,并检查它们是否具有相同的变量类型(例如它们是否都是布尔值,int等等)。



die();你忘记了分号()



你的代码应该是

  if($ _ GET ['id'] =='youtube'){
header('Location:http://www.youtube.com/');
die();
}
if($ _ GET ['id'] =='twitter'){
header('Location:http://www.twitter.com/');
die();
}
if($ _ GET ['id'] =='reddit'){
header('Location:http://www.reddit.com/');
die();
}


I am new to PHP, so please bear with me in this elementary level question.

I want to create a script that redirects the user to various addresses based on the GET variable. for example, redirection.php?id=youtube should redirect them to www.youtube.com, redirection.php?id=twitter should redirect them to www.twitter.com, and so on.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<title>Please Wait...</title>
</head>
<body>

<?php
// directs the user to various locations on the internet
print_r($_GET);

if($_GET['id'] === 'youtube') {
    header('Location: http://www.youtube.com/') ;
    die()
}
if($_GET['id'] === 'twitter') {
    header('Location: http://www.twitter.com/') ;
    die()
}
if($_GET['id'] === 'reddit') {
    header('Location: http://www.reddit.com/') ;
    die()
}
?>

</body>
</html>

So far the PHP file does not respond at all, what do I change to fix this?

Again, sorry for the elementary level question, but this is literally my first PHP script and I am not very familiar with some of the terminology which makes Google searching for the correct code difficult.

解决方案

When comparing values in PHP for equality you can use either the == operator or the === operator. What’s the difference between the 2? Well, it’s quite simple. The == operator just checks to see if the left and right values are equal. But, the === operator (note the extra "=") actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).

And

die(); you forget semicolon in die()

you code should be

if($_GET['id'] == 'youtube') {
    header('Location: http://www.youtube.com/') ;
    die();
}
if($_GET['id'] == 'twitter') {
    header('Location: http://www.twitter.com/') ;
    die();
}
if($_GET['id'] == 'reddit') {
    header('Location: http://www.reddit.com/') ;
    die();
}

这篇关于基于GET变量的PHP重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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