PHP $ _GET前一页的变量

问题描述:

当用户点击一个类别时,它将转到InsectsandPlants.php?pg=1?pg=whatevercategory。然后在下一页上我有一个项目列表,每个项目都有一个唯一的URL,类似于:view.php?aud=5,但每个项目的?aud更改。因此,这里是我的view.php代码:

<?php 

    $audint=$_GET['aud']; 
    $result=mysql_query("SELECT * FROM insects WHERE inid=$audint"); 

    while($row=mysql_fetch_array($result)) 
    { 
     $lru = $row['url']; 
     $title = $row['title']; 
?> 
<h3>You Are Viewing <?php echo $title; ?></br></br> 
<embed src="<?php echo $lru; ?>" autostart="true" loop="false" controller="true" bgcolor="#000" height="42" width="300"> 
</br></br> 
<?php 
    } 
?> 

和类别页面:

<?php 

    $pgint=$_GET['pg']; 

    switch($pgint) 
    { 
     case "1": 
      echo '<li><h4>Insects and Plants</h4>'; 
      $result=mysql_query("SELECT * FROM insects ORDER BY inid ASC"); 
     break; 
     case "2": 
      echo '<li><h4>Dr. Seuss</h4>'; 
      $result=mysql_query("SELECT * FROM DrSeuss ORDER BY inid ASC"); 
     break; 
    } 

    while($row=mysql_fetch_array($result)) 
    { 
     array_reverse($row); 
     $lru = $row['url']; 
     $title = $row['title']; 
     $id=$row['inid']; 
    ?> 

它的工作原理为,当?pg=1,因为链接到insects SQL表。但我想创建一个变量,这个变量取决于前一页的?pg=号码。

任何人都可以帮忙吗?

这听起来像任何链接需要有:

<a href="someurl.php?blah=blah&pg=<?PHP echo $_GET['PHP'];?>">...</a> 

这样$_GET['pg']将被添加到URL的查询字符串和站点随身携带。如果您有很多链接并且需要全局访问,则可能需要一些工作。

如果是这种情况,martriay的建议可能会更好(但会对服务器造成额外的负载)。

您应该将该值存储在会话变量中。

session_start(); //this must be before any output, at the beginning of the script would be great 

$_SESSION['pgnumber'] = $_GET['pg']; 

这样,在其他任何脚本中使用$_SESSION['pgnumber']变量

+0

对downvotes的解释,plz? – martriay 2013-03-01 06:09:04

您需要保存$ _GET [AUD'];在一些会话变量浏览到一些地方

下面的代码现在可以帮助使用

<?php 
session_start(); 

... some Code 

$_SESSION['aud'] = $_GET['aud']; 


... Some Code 

?> 

,当您需要使用它,你可以按照如下

<?php 
$aud = 0 ; 

if(isset($_SESSION['aud'])) 
{ 
$aud = $_SESSION['aud']; 
} 

?> 

永远不要把请求数据使用它之前直接进入sql查询。每个人都可以轻松访问数据库。从头开始创建安全页面。