我想在不使用表单的情况下将`id`传递给index.php,但它不起作用

时间:2014-03-10 09:02:36

标签: php html5

我想将id传递给index.php,我的代码如下所示。

我的main.php是:

<?php 

$rd = dirname(__FILE__); //To Get the Directory Path...
$request[]='';      // To make an empty array

if (empty($request[1]) && empty($request[2]) && isset($_SESSION['l_user']))
{

    $request[1] = 'index';
    $request[2] = '?id=1';
    include_once($rd.'/php_includes/'.$request[1].'.php'.$request[2]); //i think the problem might be here...   
}else {

    $request[1] = 'index';
    $request[2] = '?id=2';
    include_once($rd.'/ph p_includes/'.$request[1].'.php'.$request[2]);  // and might be here
}
?>

和我的index.php是这样的:

if(isset($_GET['id'] == 1)) {
           //Do something...
}else{
           //do something else...
}

但它没有任何结果,即使它没有任何错误。它不起作用。

3 个答案:

答案 0 :(得分:2)

试试这个,你只需初始化include_once的值顶部,你就可以在包含的文件中找到初始化变量(即index.php)。请注意,您无法在include ad requires

中将值作为查询字符串传递
$id = '1';
include_once($rd.'/php_includes/'.$request[1].'.php');
index.php中的

 if($id=="1"){

  }else{

  }

答案 1 :(得分:2)

您应该使用header代替require_once,因为require_once就像include一样,它会将包含的代码复制到文件中。

<?php 

$rd = "http://ip_address"; // ip_address or dns name
$request[]='';      // To make an empty array

if (empty($request[1]) && empty($request[2]) && isset($_SESSION['l_user']))
{

    $request[1] = 'index';
    $request[2] = '?id=1';
    header('Location:'.$rd.'/php_includes/'.$request[1].'.php'.$request[2]); //i think the problem might be here...   
}else {

    $request[1] = 'index';
    $request[2] = '?id=2';
    header('Location:'.$rd.'/php_includes/'.$request[1].'.php'.$request[2]);  // and might be here
}
?>

答案 2 :(得分:1)

这是示例代码还是实际代码?因为使用数组传递相同的URL而不是传递整个字符串

是不常见的
include_once($rd.'/php_includes/index.php?id=1');

你的代码可能会变成这个

<?php
$rd = dirname(__FILE__); //To Get the Directory Path...
# it seems that the session is important :)
if (isset($_SESSION['l_user']))
{
    $id=1; 
}else {
    $id=2;
}
include_once($rd.'/ph p_includes/index.php?id='.$id);
?>
相关问题