qwertymk
qwertymk

Reputation: 35334

php setting the query string

I have a form that submits to the same page. Now when it gets submitted and after it's processed I need it to have a unique query string.

So for example the user inputs some info then clicks submit, then the page saves the info on the server then the server spits it back out with a unique query string for that info.

If I try to set $_SERVER['QUERY_STRING'] it just hangs. Is there another way to do this?

Is it possible with a redirect?

EDIT, I'm going from mysite.com/ and the form action is on mysite.com/ and I want the browser to go to mysite.com/?blah

OK I tried putting this on my the top of my page with no luck

<?php
if ($_POST['data']) header('location: /?' . idFromDifferentFunction() );
?>

but it just keeps loading, I'm guessing it just redirects itself to death.

I hope you now understand what I'm trying to do

Upvotes: 1

Views: 1266

Answers (4)

David Stockton
David Stockton

Reputation: 2251

Chances are that your script is continuing to run after the code that says it should redirect. You also need to be more precise with the header:

<?php
    if (isset($_POST['data'])) {
        header('Location: /?' . idFromDifferentFunction() );
        exit;
    }
?>

If you use the code above, it will make the script exit which dumps the output and the browser will see the redirect (note the capital L in Location).

The key point is the exit following the redirect header. Without it, PHP is very likely going to continue working on whatever other code you're doing in the script.

Upvotes: 3

GordonM
GordonM

Reputation: 31780

It's not entirely clear what you're after, but I think you mean you want to go to a page with a unique value in the query string (the bit after the ?) once the processing is complete. Does this unique value need to actually reference something in the system (for a newly-created DB entry does it need to reference the ID of the new entry) or does it just have to be unique?

If it's the latter, you could just generate a random unique ID do the following:

header ('location: /path/to/script?id=' . uniqid ());

If it's the former, then replace the call to uniqid with the value of the database key.

The values in $_SERVER are set at runtime by PHP and should be considered read-only. Changing their values will have no meaningful effect.

Upvotes: 1

Marc B
Marc B

Reputation: 360862

Writing to $_SERVER is pointless. It doesn't affect the client browsers in any way. If you want to change the query string displayed in the client browser, you'll have to use a 301/302 redirect using a header('Location: ...') call.

Upvotes: 0

Paul S.
Paul S.

Reputation: 1537

$_SERVER['QUERY_STRING'] is part of PHP's globals. You should not be setting those variables, instead set it via a session and return it after submission.

If you are trying to redirect the user to a specific URL then use:

header('Location: mysite.com/bla/bla');

Upvotes: 0

Related Questions