简体   繁体   中英

Need logic: “If URL parameter exists, post same URL parameter on all webpage href links.”

I'm trying to figure out how to post URL parameters on all href links if a certain URL parameter exists. For example, say I'm on http://myWebSite.com?ID=123 , I want all internal or external href links on that page to also carry URL parameter "?ID=123".

Here are some sample href links you might find on my homepage, and with some logic, I want those links to carry the captured URL parameter.

myWebSite.com/about ---> myWebSite.com/about?ID=123
myWebSite.com/contact ---> myWebSite.com/contact?ID=123
twitter.com/cmF ---> twitter.com/cmF?ID=123

if(isset($_GET['ID']))
{
   // Post ID URL parameter to all href links
}

You can use the php function output_add_rewrite_var for this purpose.

This function adds another name/value pair to the URL rewrite mechanism. The name and value will be added to URLs (as GET parameter) and forms (as hidden input fields) the same way as the session ID when transparent URL rewriting is enabled with session.use_trans_sid. Please note that absolute URLs ( http://example.com/. .) aren't rewritten.

This can be used like:

<!-- index.php -->
<?PHP
if(isset($_GET['id'])) {
    output_add_rewrite_var("id", $_GET['id']);
}
?>
<a href="./test.php">Test url</a>

PHP will rewrite this to the following when calling the script via index.php?id=1234 :

<a href="./test.php?id=1234">Test url</a>

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