We help IT Professionals succeed at work.

Detect # in URL With PHP

jls33fsls asked
Last Modified: 2008-07-08
I have created a section on my site that has tabs with different content for different pages in them.  I identify them by #id, so that if the user refreshes the page it keeps them on that "id" page instead of going back to the first one.  However, since I switch it over with javascript, it shows the first page for a second while it loads and then switches over to the correct page.  I was wondering if there is a way to do this same thing in PHP.
var the_block = window.location.href.split("#");
		if (the_block[1] == "auctions") {
			document.getElementById('market').style.display = "none";
			document.getElementById('auctions').style.display = "block";
			document.getElementById('market_link2').style.fontWeight = "normal";
			document.getElementById('auctions_link2').style.fontWeight = "bold";
		} else if (the_block[1] == "trades") {
			document.getElementById('market').style.display = "none";
			document.getElementById('trades').style.display = "block";
			document.getElementById('market_link3').style.fontWeight = "normal";
			document.getElementById('trades_link3').style.fontWeight = "bold";
		} else if (the_block[1] == "shop") {
			document.getElementById('market').style.display = "none";
			document.getElementById('shop').style.display = "block";
			document.getElementById('market_link4').style.fontWeight = "normal";
			document.getElementById('shop_link4').style.fontWeight = "bold";
				if (the_block[2] == "stock") {
					document.getElementById('stock').style.display = "block";
					document.getElementById('inventory').style.display = "none";
					document.getElementById('stock_link').style.fontWeight = "bold";
					document.getElementById('inventory_link').style.fontWeight = "normal";
				} else if (the_block[2] == "history") {
					document.getElementById('history').style.display = "block";
					document.getElementById('inventory').style.display = "none";
					document.getElementById('history_link').style.fontWeight = "bold";
					document.getElementById('inventory_link').style.fontWeight = "normal";
				} else {

Open in new window

Watch Question

Top Expert 2007
This one is on us!
(Get your first solution completely free - no credit card required)
Ali KayahanFull Stack Developer

You may use parse_url() function to get anchor side on a given url as string. You may use ;
$url = 'http://username:password@hostname/path?arg=value#anchor';


echo parse_url($url, PHP_URL_PATH);
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor

But as hernst42 said you should use JS before...


Well, I thought about doing a cookie, but the problem with this is that if they leave that section and come back it won't go to the first page like they expect.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.