-2

On my website (thirdsidethreads.com) I want to display a "welcome" message on the index page, but I don't want it displayed when people access the index page with certain parameters in the URL. If the URL is plain and only includes the domain, it means it's functioning as the home page. If there are other characters in the URL, that means the visitor is trying to use that page to view something specific and it's not the home page.

In other words, I want this:

If the URL doesn't have anything except the domain name in it, display the message.

If the URL looks like this, don't display it: http://www.thirdsidethreads.com/#!1007747370?q=I1007747370

I'd appreciate it if someone could give me a solution for this. I feel like I've tried everything.

Community
  • 1
  • 1
Matt
  • 1
  • 1
  • Please show us what you've tried and what specifically goes wrong. – showdev Jul 21 '16 at 23:11
  • You can use the global variable $_GET. I will attempt to create some code and post it when I am done with dinner. – Teddy Codes Jul 21 '16 at 23:11
  • I tried using $_GET and for some reason it won't work. I've also tried the basic PHP functions for echoing a URL and they never get past the file name. The part of the URL that I need to use for this purpose starts at "/#!" If I can detect that in the URL I should be good. – Matt Jul 21 '16 at 23:28

2 Answers2

0

You should to start with parse_url() and pass in the current URL for your page.

You'll be interested in three parts of the output; the path, the query and the fragment. Path can be a slash if your URL is example.com/, so ignore that too and do something like this:

$showMessage = ((empty($bits['path']) || $bits['path'] === '/') && empty($bits['query']) && empty($bits['fragment']));

I've added an example here with a couple of assertions. Feel free to expand the tests to see if it actually will suit your use case.

To target the URL's fragment specifically, you can use the PHP_URL_FRAGMENT constant as an argument:

$fragment = parse_url($url, PHP_URL_FRAGMENT); // !1007747370?q=I1007747370 in your example
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Is there a way to just detect the "#!" in the URL, and only show the message if the URL doesn't contain it? – Matt Jul 21 '16 at 23:34
  • @MattDeLaRoche yes, you would use the `fragment` component of that method's return. You can also target it specifically via `$fragment = parse_url($url, PHP_URL_FRAGMENT)`. It will contain everything in the anchor other than the anchor `#` itself. – scrowler Jul 21 '16 at 23:46
  • The thing is, I don't know how to retrieve the full URL in the first place. All I've been able to do is retrieve the domain and file name, and nothing more, so I don't know how I'd get the $url variable. – Matt Jul 21 '16 at 23:49
  • You [can't get it with PHP](http://stackoverflow.com/questions/940905/can-i-read-the-hash-portion-of-the-url-on-my-server-side-application-php-ruby), but you could create the alert with a CSS style to hide it and use Javascript to read the value in the URL and show the alert if necessary – scrowler Jul 21 '16 at 23:58
  • I tried this and it didn't work. I don't know what to do. $url = ""; $fragment = parse_url($url, PHP_URL_FRAGMENT); echo $fragment; – Matt Jul 22 '16 at 00:17
  • You can't do that :) Javascript executes when the page is loaded, PHP executes before the page has loaded. – scrowler Jul 22 '16 at 02:00
0

Since it appears to be impossible to retrieve the hash tag part of my URLs with PHP, I went with a JavaScript solution instead. It works fine.

<script>
function message() {

if (window.location.hash=="") {

    document.getElementById('welcome').style.display="block";

} else {

    document.getElementById('welcome').style.display="none";

}

}

window.onload=message;
</script>
Matt
  • 1
  • 1