开发者

PHP Registration Problem

开发者 https://www.devze.com 2023-03-25 11:39 出处:网络
I am programming a php registration system where there is two points of entry (if that makes sense): If the client wants to purchase a product but doesn\'t have an account and it stores the id of th

I am programming a php registration system where there is two points of entry (if that makes sense):

  1. If the client wants to purchase a product but doesn't have an account and it stores the id of the product all the way through the registration process. Then it allows to process the payment after registration.

  2. The client is registering first time without trying to purchase the product.

These are the links directing the user to the registration pages for the two methods above:

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

This is the code for the file 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>

The problems are:

  1. When using the 1st method above the php says it has a redirect loop and i can't seem to work it out. Once we have this working i want is to show the content but this leads on to the second problem.

  2. When using the second method it works and shows the content for first registration but it doesn't show any of the html content below this code.

Any help would be appreciated.

Thanks

UPDATE:

T开发者_高级运维hanks to your answers i have rewritten it like this and it works now.

<?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

}
?>


His update will work

<?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");

}
?>

Because it is what i have worked out myslf


Indenting your files more accurately may be of help in this case:

<?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>

There is no way for the system to reach the html. If $_GET["type"] is not empty, the first if or the nested if gets called and the script exits. If $_GET["id"] is not empty, the nested if might be executed and the script exits. If $_GET["id"] is empty the the final else is executed and the script exits.

This line is very interesting:

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

This says: 'is type empty? then empty($_GET['type'] is true. True == "first" is also true, so the script exits. Is this what you intended? It seems like you meant

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

To get to your situations 1 and 2:

Situation 1, if it comes back to this page, goes into the final else. This adds &step=1 to the url. But that does not change the situation, so you keep getting to this else and redirecting to the same url. I don't think you intend to go to this else once you have added &step=1, or do you?

Situation 2 goes into the second if, because $_GET["type"] is not empty but $_GET["id"] is, and then executes the nested if's direct exit.

I think you sort of meant this:

<?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>


Problems:

  1. the redirect loop -- say the link looks like this when rendered: A. B. When the user clicks it, it goes to URL: http://yourhostname/register.php?id=1234 C. It hit's the 3rd clause on register.php. $product_id=1234. D. You redirect the user's browser to http://yourhostname/register.php?id=1234. E. The user's browser goes there, and experiences basically goes straight to C. Over and over and over.

  2. The second method doesn't show any HTML code because it's not supposed to -- a redirect is the last thing you're supposed to send in response to any page request -- your use of "exit" is correct.

Solution: Why are you redirecting at all? You could replace the whole block with this:

<?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
?>


what if i goto step 10

register.php?type=first&step=10

you need to specify valid steps and types e.g

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

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

using empty() is not a good idea

0

精彩评论

暂无评论...
验证码 换一张
取 消