1

I am building a website and I have come across something that I do not know how to do.

I have a link on every page that members of my website can click, and when they do it takes them to the same page with a query on the end, and a confirmation link like;

/contact?tab=hotlink

Now on this page is a hidden form and a confirm button, I was going to use $_SERVER['HTTP_REFERER'], but the previous url is /contact?tab=hotlink and I need it to be...

/contact

Is there a way to string replace all of the query, I could do it with sessions, but would rather do it another way as it would mean setting alot of unnessessary sessions.

Thanks

4 Answers 4

3

The querystring is in

$_SERVER['QUERY_STRING']

So

str_replace( $_SERVER['QUERY_STRING'], '', $_SERVER['HTTP_REFERER'] )

should give you the request without the query string.

Sign up to request clarification or add additional context in comments.

Comments

2

Use the php explode function to extract the base url from the referer by splitting the string on the ?:

$http_referer_full_url = explode("?", $_SERVER["HTTP_REFERER"]);
$http_referer_base_url = $http_referer_full_url[0];

@Rijk's answer doesn't work because $_SERVER['QUERY_STRING'] in the example refers to the script currently executing, not the query string from the referrer page.

Comments

0

Usually you wouldn't do a page refresh and a querystring for that - you would link to a #hashed url instead, like this:

<a href="/contact#tab=hotlink">Hotlink</a>

You'd then use a Javascript handler to catch the hashchange event (which doesn't require a page refresh, so it is much faster) and dynamically load/display whatever you need on the 'hotlink' tab.

That way, the server wou't even be involved and your pages will be a lot more responsive.

2 Comments

And if javascript is disabled?
If you are very concerned about the tiny fraction of no-js users out there, you will need to jump through some hoops, of course. You could degrade the functionality in plenty of ways - use the body class='no-js' trick with css, or inject the #hashes with Javascript, for example
0

You can use $_SERVER['SCRIPT_NAME'] for this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.