简体   繁体   中英

how do i make my ahref link dynamic? for creating dynamic url parameters

i am trying to pass the visitors ip adress at the end of three links on my web page. so the link needs to be dynamic.

can i do something like the following?

<?php


//ip shared int
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
    $IP = $_SERVER["HTTP_CLIENT_IP"];
}
else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
    //CHECK PROXY IP
    $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
    $IP = $_SERVER["REMOTE_ADDR"];
}

echo $IP;

<a href="http://lnkclik.com/8vMH/$IP">GET A $100 Panda Express Giftcard</a>


?>

Your code should look like this:

<?php

//ip shared int
if(!empty($_SERVER["HTTP_CLIENT_IP"]))
{
    $IP = $_SERVER["HTTP_CLIENT_IP"];
}
else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
    //CHECK PROXY IP
    $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
else
{
    $IP = $_SERVER["REMOTE_ADDR"];
}

echo "<a href='http://lnkclik.com/8vMH/{$IP}'>GET A $100 Panda Express Giftcard</a>";


?>

Is that what you wanted?

<?php


//ip shared int
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
    $IP = $_SERVER["HTTP_CLIENT_IP"];
} else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
    //CHECK PROXY IP
    $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
    $IP = $_SERVER["REMOTE_ADDR"];
}

echo $IP;
echo "<a href=\"http://lnkclik.com/8vMH/$IP\">GET A $100 Panda Express Giftcard</a>";
?>

So basically when you're making a request to your webserver, php will compile the code and return html to the client. In this case, what you're trying to do is output html inside your php tags, so the compiler will think that's part of your php code (which it isn't) and you'll get a fatal error (application will crash).

<?php

//ip shared int
if (!empty($_SERVER["HTTP_CLIENT_IP"])) {
    $IP = $_SERVER["HTTP_CLIENT_IP"];
} else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) {
    //CHECK PROXY IP
    $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else {
    $IP = $_SERVER["REMOTE_ADDR"];
}
<a href="http://lnkclik.com/8vMH/$IP">GET A $100 Panda Express Giftcard</a>


?>

<a href="http://lnkclick.com/8vMH/<?= $IP; ?>">GET A $100 Panda Express Giftcard</a>

<?= "Outputs this value"; ?> <?= "Outputs this value"; ?> is the same as <?php echo "Outputs this value"; ?> <?php echo "Outputs this value"; ?>

Hope this helps =)

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