带变量的 PHP 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18436941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:34:34  来源:igfitidea点击:

Php redirect with variable

php

提问by st15jap

I am trying to use

我正在尝试使用

  header("Location: http://www.mysite.com/gamecode.php?gameid=$pw");

below is my code.

下面是我的代码。

   <?php
header("Location: http://www.mysite.com/gamecode.php?gameid=$pw");
$con = mysqli_connect("localhost","placeholder","placeholder","placeholder");
 //or die ('unable to connect');

 // Check connection
 if (mysqli_connect_errno())
 {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


 //create random table name

  $alpha = "abcdefghijklmnopqrstuvwxyz";

  $numeric = "0123456789";
  $special = ".-+=_,!@$#*%<>[]{}";
  $chars = "";

  if (isset($_POST['length'])){
  // if you want a form like above
  if (isset($_POST['alpha']) && $_POST['alpha'] == 'on')
    $chars .= $alpha;

if (isset($_POST['alpha_upper']) && $_POST['alpha_upper'] == 'on')
    $chars .= $alpha_upper;

if (isset($_POST['numeric']) && $_POST['numeric'] == 'on')
    $chars .= $numeric;

if (isset($_POST['special']) && $_POST['special'] == 'on')
    $chars .= $special;

$length = $_POST['length'];
}else{
// default [a-zA-Z0-9]{9}
$chars = $alpha . $numeric;
$length = 6;
 }
$len = strlen($chars);
$pw = '';

 for ($i=0;$i<$length;$i++)
    $pw .= substr($chars, rand(0, $len-1), 1);
  // the finished password
 $pw = str_shuffle($pw);
 //using the $pw variable for the table name
 $sql="CREATE TABLE `" . $pw . "` (
 PID INT NOT NULL AUTO_INCREMENT, 
 PRIMARY KEY(PID),
 Name CHAR(15))";
 if (mysqli_query($con,$sql))
 {
 echo "table was created";
  }
  else
 {
  echo "Did not create table";
 }



  mysqli_close($con);

above is the page i am trying to add the variable to the URL. I do get the the page redirect however it is not adding the header variable to the redirect as it should.

上面是我试图将变量添加到 URL 的页面。我确实得到了页面重定向,但是它没有像它应该的那样将标头变量添加到重定向中。

回答by Sudhir Bastakoti

use double quotes instead of single quote, so change to:

使用双引号代替单引号,因此更改为:

header("Location: http://www.yoursite.com/new_page.php?gameid=$gmid");

or

或者

header('Location: http://www.yoursite.com/new_page.php?gameid='.$gmid);

Double quotes, will evaluate variable values before producing the final output, which single quote does not.

双引号将在生成最终输出之前评估变量值,而单引号则不会。

回答by Sudhir Bastakoti

try this:

尝试这个:

header('Location: http://www.yoursite.com/new_page.php?gameid=' . $gmid);

回答by Gautam3164

Try like

试试像

header('Location: http://www.yoursite.com/new_page.php?gameid='.$gmid);

You need to change the quotes before the variable or You can also try with double quoteslike

您需要更改变量前的引号,或者您也可以尝试使用double quoteslike

header("Location: http://www.yoursite.com/new_page.php?gameid=$gmid");

回答by slash197

Change your quotes to double quotes.

将引号更改为双引号。

回答by itachi

header("Location: http://www.yoursite.com/new_page.php?gameid=$gmid");

header("Location: http://www.yoursite.com/new_page.php?gameid=$gmid");

see the quotes.... "instead of '.

请参阅引号...."而不是'.

回答by Charaf JRA

You have problem with quotes and double quotes: Change your line to this :

您有引号和双引号的问题:将您的行更改为:

 header("Location: http://www.yoursite.com/new_page.php?gameid=$gmid");

or this:

或这个:

 header("Location: http://www.yoursite.com/new_page.php?gameid=".$gmid);

If you do '$variable', it will be interpreted as string and :

如果这样做'$variable',它将被解释为字符串和:

echo '$variable'; will show $variableas output . But if you use "$variable" , echo will output value of this variable.

回声'$variable'; 将显示$variable作为输出。但是如果你使用 "$variable" ,echo 会输出这个变量的值。