views:

61

answers:

1

Hello,

I use a index.php to control the access of pages.

Here is the mechanism:

Index.php redirects the page to search.php; code: index.php/?page=search the path of search.php is : pages/search.php, search.php includes header.html, body.html etc.

Now in body.html we call search.php using: index.php/?page=search parameter, and body.html includes this code: index.php/?page=search?query='.quote_replace(addmarks($query)). You can see there are two '?'. And it's just this problem.

When i browse search.php, it displays in url: xxx.com/?page=search.

When i click the button in search.php while actually in body.html, it displays in url xxx.com/index.php/?query=&search=1.

In this way, the browse just display page index.php. But i want to display the search result in search.php, and it should display xxx.com/?page=search/?query=&search=1. But you can see /?page=search disappears and /index.php appears here.

I'm sorry, the condition is complex and i don't describe it very well. If you couldn't understand it very well, i'll explain.

So, do you have any advice about that? Thanks a lot.

+2  A: 

If you want to use a string (such as search/?query=&search=1) as a URL argument, you need to encode so that it no longer contains special charachters such as ? and &. Use the urlencode() PHP function for this:

urlencode("search/?query=findme&search=1")

This will yield the following string, which can be safely included as a URL argument:

search%2F%3Fquery%3Dfindme%26search%3D1

To build the first string you need to so something similar, i.e. encode all arguments. Here the http_build_query() function can be useful, which takes an array with keys and values, encodes all of them and adds the = and & characters where needed:

http_build_query(array("query" => "find&me", "search" => "1"))

which yields

query=find%26me&search=1
Wim
@thanks Wim, but i haven't used this function. I will try.
garcon1986
@Wim, Your advice is so good. Do i need to use urlencode and http_build_query together? I'm sorry, i have never used it before. And i have no idea at all right now.
garcon1986
You could also use `urlencode` for the second case and construct the query string yourself, but `http_build_query` is so much easier. And in any case, you'll need to do two sets of encoding on the same data, because it will also be *decoded* twice (once when the value for *page* is read, and once again when *query* and *search* are read).
Wim