简体   繁体   中英

Whats the best way to do a redirect in PHP with a hashtag?

After a successful authentication I send the user to the home page by calling header

if($success) {
    header('Location: home.php');
    exit();
}

Ulitimatley I'd like to use a hashtag as part of the URL so for instance the URL to the user would look like this:

mysite/#!home

Can I do something like this or is there a better way to redirect them to the home page after a successful login?

That sort of hash tag is usually used with what is called, "addressing" or "deep linking" , a type of javascript page navagation. What you want is URL Rewriting, which allows you to do things like www.website.com/home

Deep Linking : http://www.asual.com/jquery/address/

Url rewrite: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Because this :

if($success) {
    header('Location: yoursite/#!home');
    exit();
}

is not working ?

You can also pass a parameter to the successful page, and fire off some javascript to anchor the user down if that parameter is set:

<script type="text/javascript">
    <?php if (isset($_GET['val'])) { ?>
        // redirect to the appropriate hash from parameter
        document.location.hash = "<?php echo $_GET['val'] ?>";
    <?php } ?>
</script>

This won't give you a clean URL, but its a quick fix if you don't have access to any URL rewriting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM