Search code examples
phpvariablesstrpos

PHP variable with strpos based on dynamic hash url


I am dynamically adding a hash to a url - taking that strpos and trying to display a certain footer based on that variable, I know I could just translate the whole page but I need to do this for a client, why isn't it working I suppose is the question?

The url looks like http://www.example.com/#/sp/

Here is my header:

<?php 
    $page = $_SERVER['REQUEST_URI'];
    $isSpanish = strpos($page, '/sp/'); 
    $isSpanishDynamic = strpos('#/sp/', $page);

Here is my footer:

   <?php if($isSpanishDynamic !== FALSE || $isSpanish !== FALSE) { ?>
                    <li><a href="/">PÁGINA DE INICIO</a></li>
                    <li><a href="/sp/our-story">HISTORIA</a></li>
                    <li><a href="/sp/menu">MENÚ</a></li>
                    <li><a href="/sp/contact">CONTACTO US</a></li>
                    <li><a href="/sp/order-online">ORDENE POR INTERNET</a></li>
    <?php } else { ?>
                    <li><a href="/">Home</a></li>
                    <li><a href="/en/our-story">Our Story</a></li>
                    <li><a href="/en/our-menu">Our Menu</a></li>
                    <li><a href="/en/contact">Contact Us</a></li>
                    <li><a href="/en/order">Order Online</a></li>
    <?php } ?>  

Solution

  • In a url like http://example.com/#/sp/, the hash #/sp/ will never be sent to the server, so you cannot do anything with in PHP unless you interact with the server somehow.

    See Wikipedia:

    The fragment identifier functions differently than the rest of the URI: namely, its processing is exclusively client-side with no participation from the server. When an agent (such as a Web browser) requests a resource from a Web server, the agent sends the URI to the server, but does not send the fragment. Instead, the agent waits for the server to send the resource, and then the agent processes the resource according to the fragment value. In the most common case, the agent scrolls a Web page down to the anchor element which has an attribute string equal to the fragment value. Other client behaviors are possible