getting full URL in php without white spaces

眉间皱痕 提交于 2020-02-23 08:41:53

问题


I am trying to share the full URL from my website to whatsapp using mobile browser, I tried this to retrieve the full URL and it works perfectly when I echo the variable, but when I share using the mobile browser, it gives me the link with white space like this: http://127.0.0.1/?id= 40
which can not be opened, how can I get the full echoed link without no spaces?

   <?php
    $alink= (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ?

            "https" : "http") . "://" . $_SERVER['HTTP_HOST'] .

        $_SERVER['REQUEST_URI'];
    ?>
    

//To Share
    <a href="whatsapp://send?text=<?php echo $message['title'] ?> <?php echo "\r\n" ?> <?php echo $alink ?> <?php echo "\r\n" ?> <?php echo $myText ?>"
                           data-action="share/whatsapp/share">Share To Whatsapp</a>

回答1:


$alink = str_replace(' ', '', $alink)

Manual




回答2:


the standard conditions [$_SERVER['HTTPS'] == 'on'] do not work on servers behind a load balancer:

$isSecure = false;
    if (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
        $isSecure = true;
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_SERVER['HTTP_X_FORWARDED_SSL']) && $_SERVER['HTTP_X_FORWARDED_SSL'] == 'on') {
        $isSecure = true;
    }
    $REQUEST_PROTOCOL = $isSecure ? 'https' : 'http';

    echo $REQUEST_PROTOCOL."://".$_SERVER['HTTP_HOST'];

I think this will Help You and please refer PHP documentation




回答3:


Get the full url by javascript with window.location.href iny my file.php

...
<div id="url"></div>
...
<script type="text/javascript">
   document.querySelector('#url').innertHTML = window.location.href
</script>


来源:https://stackoverflow.com/questions/60069643/getting-full-url-in-php-without-white-spaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!