Jump to content

Remove A querystring from url


Wildhalf

Recommended Posts

Anyone know how i can remove a querystring from url.

 

Example URL: http://www.mysite.com/?id=1

 

I am writing a script that needs to read in the ID from the URL. But once have got the information i would like to remove it from the URL and just display http://www.mysite.com but still be able to use the ID to query my database.

 

Any ideas???

Link to comment
https://forums.phpfreaks.com/topic/216568-remove-a-querystring-from-url/
Share on other sites

Anyone know how i can remove a querystring from url.

 

Example URL: http://www.mysite.com/?id=1

 

I am writing a script that needs to read in the ID from the URL. But once have got the information i would like to remove it from the URL and just display http://www.mysite.com but still be able to use the ID to query my database.

 

Any ideas???

 

You can rebuild it using the server vars.  Have a look at:

 

print_r($_SERVER);

 

Use the appropriate values to rebuild it without the query string.

If you want to read it then remove it, you need to store the value in a cookie or session var before redirecting.  Typically its best to avoid situations like this, because its not good for tabbed browsing.  E.g. If I open ?id=1 in one tab, create a new tab and open ?id=2, the first tab would then use id=2 rather than id=1 like it should.

 

So, I think you should ask yourself if its really necessary.  (Also, what if users have cookies turned off? You could transfer a session ID through the URL, but that's usually not a good thing to do.)

I am only going to be using the ID on one page. Say i have 10 different ID's 1 to10. All i am doing is when i send a user to ?ID=1 it displays one thing taken from my database. ?ID=2 displays something else and so on.

 

I suppose i dont now enough about php to do it but i know what i want done works as i have seen it with a few scripts.

Depends on what you mean by "remove it" from the URL.  Do you mean in the browser location bar or what?  Why do you need to remove it?  Normally this is what you do to get the ID:

 

page1.php

echo '<a href="page2.php?ID=1">Click for ID 1</a>';

 

page2.php

echo 'The ID is: ' . $_GET['ID'];

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.