问题
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