2011-08-03 121 views
1

我编程一个PHP登记制度,其中有两个入口(如果是有道理的):PHP注册问题

  1. 如果客户想要购买产品,但没有一个帐户,并它通过注册过程一直存储产品的ID。然后它允许在注册后处理付款。

  2. 客户端首次注册时未尝试购买产品。

这些是将用户引导到注册页两种方法上面的链接:

1. <a href="register.php?id=<?php echo $row['product_id'] ?>"></a> 
2. <a href="register.php?type=first"></a> 

这是文件register.php的代码:

<?php 
if(empty($_GET['type'])== "first") { 
    header("Location: register.php?type=first&step=1"); 
} 
if(empty($_GET['id'])) { 
    if($_GET['type']) { 
     //show the content first registration 
} 
    header("Location: register.php?type=first&step=1"); 

}else{ 
    $product_id=$_GET['id']; 
    header("Location: register.php?id=".$product_id."&step=1"); 
    //show the content with the product information 
} 
?> 

<html>...</html> 

的问题是:

  1. 当使用上面的第一种方法时,php说它有一个重定向循环,我似乎无法解决这个问题。一旦我们有这个工作,我想要显示的内容,但这导致第二个问题。

  2. 当使用第二种方法时,它工作并显示首次注册的内容,但它不显示此代码下面的任何html内容。

任何帮助,将不胜感激。

感谢

UPDATE:

感谢你的答案我已经重写它像这样和现在的工作。

<?php 
if($_GET['type'] == "first" && empty($_GET['step'])) { 
    header("Location: register.php?type=first&step=1"); 

    //If user clicks on (<a href="register.php?type=first"></a>) 

    //show the content first registration 

}elseif(empty($_GET['id']) && empty($_GET['type'])) { 
    header("Location: register.php?type=first&step=1"); 

    //if user types in the address bar www.domain.co.uk/register.php 

    //redirect and show the content first registration 

}elseif(isset($_GET['id']) && empty($_GET['step'])){ 
    $product_id=$_GET['id']; 
    header("Location: register.php?id=".$product_id."&step=1"); 

    //if the user clicks on <a href="register.php?id=<?php echo $row['product_id'] ?>"></a> 

    //show the content with the product information 

} 
?> 
+1

你总是要退出脚本,所以下面的代码不执行过程中(HTML) – Grumpy

+0

@脾气暴躁好吧,问题是如果我退出它退出它继续与代码的其余部分是否有任何方法跳转到HTML在这一点上? –

+1

@David Passmore你应该适当地嵌套if-elses,例如elseif或case。 – Quasdunk

回答

1

他的更新将工作

<?php 
if($_GET['type'] == "first" && empty($_GET['step'])) { 
    header("Location: register.php?type=first&step=1"); 


}elseif(empty($_GET['id']) && empty($_GET['type'])) { 
    header("Location: register.php?type=first&step=1"); 


}elseif(isset($_GET['id']) && empty($_GET['step'])){ 
    $product_id=$_GET['id']; 
    header("Location: register.php?id=".$product_id."&step=1"); 

} 
?> 

因为这是我所制定了myslf

1

缩进你的文件更准确地可能会有所帮助在这种情况下:

<?php 
if(empty($_GET['type'])== "first") 
{ 
    header("Location: register.php?type=first&step=1"); 
    exit; 
} 
if(empty($_GET['id'])) 
{ 
    if($_GET['type']) 
    { 
     exit;  //show the content first registration 
    } 
    header("Location: register.php?type=first&step=1"); 
    exit; 
} 
else 
{ 
    $product_id=$_GET['id']; 
    header("Location: register.php?id=".$product_id."&step=1"); 
    exit;    //show the content with the product information 
} 
?> 

<html>...</html> 

有没有办法让系统达到的HTML。如果$ _GET [“type”]不为空,则第一个if或nested被调用并且脚本退出。如果$ _GET [“id”]不为空,则嵌套的if可能会被执行并且脚本退出。如果$ _GET [“id”]为空,则执行最后的else并且脚本退出。

这行很有趣:

if(empty($_GET['type'])== "first") 

这是说:“是n型空?然后清空($ _ GET ['type']为true。True ==“first”也是如此,所以脚本退出了。空($ _ GET [ '型'])& & $ _GET [ '型'] == “第一”)

要进入您的情况1和2:

情况1,如果回来这个页面,进入最后的其他部分。这增加了& step = 1到url。但是这并没有改变这种情况,所以你不断地去做这件事,并重定向到相同的网址。我不认为你打算一旦你加入& step = 1就去这个其他人,还是你呢?

如果因为$ _GET [“type”]不为空而$ _GET [“id”]为,然后执行嵌套的if直接退出,情况2会进入第二种状态。

我觉得你有点意思的:

<?php 
if (!empty($_GET['step']) && (!empty($_GET['type']) || !empty($_GET['id']))) 
{ 
    // This is when the html should be shown, right? 
    // So no code here 
} 
/*elseif ($_GET['type'] == "first") 
{ 
    header("Location: register.php?type=first&step=1"); 
    exit; 
}*/ // Don't need this part? 
elseif (!empty($_GET['id'])) 
{ 
    $product_id=$_GET['id']; 
    header("Location: register.php?id=".$product_id."&step=1"); 
    exit; 
} 
else 
{ 
    header("Location: register.php?type=first&step=1"); 
    exit; 
} 
?> 

<html>...</html> 
+0

我不认为我解释清楚不够清楚我已更新我的问题与我的解决方案感谢帮助虽然 –

0

问题:

  1. 的重定向循环 - 说的链接看起来像这样渲染时: A. B.当用户点击它,它转到URL:http://yourhostname/register.php?id=1234 C.它是register.php中的第3个子句。 $ PRODUCT_ID = 1234。 D.您将用户的浏览器重定向到http://yourhostname/register.php?id=1234。 E.用户的浏览器去那里,经验基本上直接转到C.一遍又一遍。

  2. 第二种方法不会显示任何HTML代码,因为它不应该是 - 重定向是您应该发送的最后一件事情,以响应任何页面请求 - 您使用“退出”是正确的。

解决方案: 为什么你要重定向?你可以用这个代替整个街区:

<?php 
$type = 'first'; 
$step = 1; 
$productId = null; 

if (!empty($_GET['type'])) { 
    $type = $_GET['type']; 
} 
if (!empty($_GET['step'])) { 
    $step = $_GET['step']; 
} 
if (!empty($_GET['id'])) { 
    $product_id = $_GET['id']; 
} 

... now add your business logic code 
?> 
+0

我不'我想我已经解释清楚了,我已经更新了我的解决方案,感谢您的帮助,虽然 –

0

如果我转到步10

register.php?type = first & step = 10

你需要t Ø指定有效的步骤和类型,例如

if($_GET['type'] == "first") { do something } 

if($_GET['step'] == 1) { do something } 
使用 空(

是不是一个好主意

+0

好点。我已经改变它只接受步骤1 2和3再次感谢 –