getting full URL in php without white spaces - javascript

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>

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

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

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>

Related

How to include php function in javascript(Alertify.js) that had been echo in php

I am using alertify.js library to prompt a cormfirmation to user. Ex if user press ok , it will run certain php funtion. The alert are echo using php. but it return an error.
<?php
include('header.php');
if( isset($_GET['id']) && $_GET['action'] == 'delete'){
$id = $_GET['id'];
echo '<script type="text/javascript">
alertify.confirm("Confirm Title", "Confirm Message", function(){ alertify.success("Ok")'.
if($user->deleteAccount($id)){
$_SESSION['current_url'] = $_SERVER['PHP_SELF'];
/* echo "<script type='text/javascript'>
window.location.replace('clear_Post.php');
</script>";*/
}
.' }
, function(){ alertify.error("Cancel")});
</script>';
}
?>

Special Characters encoding in textareas

I'm pulling contents from text files into a textarea to be used and noticed it appeared that slashes were appearing near quotes and apostrophes. I was able to resolve that by disabling magic quotes on the server, however I noticed that special characters still don't seem to display properly.
What I am trying to figure out is there a way when retrieving the file to decode/encode them properly or to encode them so they're UTF 8 compliant in the first place? Below is my coding for retrieving the files:
<?php
$directory = $directory = 'users/' . $_SESSION['username'];
$filesContents = Array();
$files = scandir( $directory ) ;
foreach( $files as $file ) {
if ( ! is_dir( $file ) ) {
$filesContents[$file] = file_get_contents($directory , $file);
echo '<option value="'. $file .'">' . $file . '</option>';
}
}
?>
</select>
and below is my save script:
if($_POST['Action'] == "SAVE") {
// If a session already exists, this doesn't have any effect.
session_start();
// Sets the current directory to the directory this script is running in
chdir(dirname(__FILE__));
// Breakpoint
if( empty($_SESSION['username']) || $_SESSION['username'] == '' ) {
echo 'There is no session username';
}
if( empty($_POST['CodeDescription']) || $_POST['CodeDescription'] == '' ) {
echo 'There is no POST desired filename';
}
// This is assuming we are working from the current directory that is running this PHP file.
$USER_DIRECTORY = 'users/'.$_SESSION['username'];
// Makes the directory if it doesn't exist
if(!is_dir($USER_DIRECTORY)):
mkdir($USER_DIRECTORY);
endif;
// Put together the full path of the file we want to create
$FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.txt';
if( !is_file( $FILENAME ) ):
// Open the text file, write the contents, and close it.
file_put_contents($FILENAME, $_POST['Code']);
endif;
header('Location: mysite.site/evo/codesaveindex.php?saved=1&file='.$FILENAME);
}
?>

Mobile detection failing

I am trying to detect if my user is using a smartphone. I have tried so many different methods but nothing seems to detect if it is a smartphone.
The one below does not even echo $_SERVER['HTTP_USER_AGENT'];
<?php
echo $_SERVER['HTTP_USER_AGENT'];
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
header('Location: http://mobile.site.com/');
}
?>
This is another script I tried that did not echo anything back.
<script type="text/javascript" >
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
<?php
echo "I should redirect";
?>
}
</script>
I have also tried getting the screen size, but that does not show anything.
<script type="text/javascript">
document.write(screen.width);
if (screen.width <= 800) {
window.location = "http://m.domain.com";
}
</script>
I have also tried the very popular php script below, but it also echo's nothing for $detect.
<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
echo $detect;
if($detect->isMobile()) {
header('Location: http://mobile.example1.com/');
exit;
}
?>
Please check that solution:
if( strstr($_SERVER['HTTP_USER_AGENT'],'Android') ||
strstr($_SERVER['HTTP_USER_AGENT'],'webOS') ||
strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') ||
strstr($_SERVER['HTTP_USER_AGENT'],'iPod')
){}

Javascript/PHP ignore quotes in string

I'm making a music player using php and javascript. I list the files like this:
<?php
if (isset($_GET["action"])) {
$action = htmlspecialchars($_GET["action"]);
if ($action == "listen") {
function listFolderFiles($dir) {
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
if($ff != '.' && $ff != '..'){
echo '<li>'. $ff . '';
if (is_dir($dir . '/' . $ff)) listFolderFiles($dir . '/' . $ff);
echo '</li>';
}
}
echo '</ol>';
}
listFolderFiles("music");
}
} else {
echo '> listen';
}
?>
And I change the song like this:
<script>
function changesong(url) {
$("#audioplayer").attr("src", url);
$("#audioplayer").trigger('play');
}
</script>
The problem is that songs with quotes in them won't play (for example Don't Stop Me Now). Is there an easy way to fix this?
You should use addslashes(), like this:
echo '<li>'. $ff . '';
You can escape quotes for javascript function in HTML code like this:
Note that you probably need to escape double quotes as well, since they can interfere with HTML tag.
$link = $dir . '/' . $ff;
$link = str_replace("'", "&apos;", $link);
$link = str_replace('"', """, $link);
echo '<li>'. $ff . '';

Should be simple (but it isn't): Javascript snippet inside PHP

I have coded in PHP and JS for a while, but I don't do it often, and so I've been snagged by something that should be simple, but it's giving me fits. And none of the answers I've found here have solved the problem.
I have JS slideshow code that is inserted into a WordPress page when multiple images named [pagename]1,2,3...png are present in the images folder. I've struggled to get the code to read the folder, and now it does. But the JS slideshow is not working on the "Menu" page, while the exact same code is working on the "Home" page. The only difference between the two is that the Menu code tests whether images are present in the images folder. (They are.)
This first part of the code is, I feel certain, correct. The second piece of code is where the issue is. Why is the JS not starting my slideshow? It should. The code reads properly on the page.
<?php
$uploads = wp_upload_dir();
$x = 1;
$poname = $post->post_name;
if ($dir = opendir('/home/rowlandwilliams/public_html/lepeep/wp-content/themes/twentythirteen-child/images')) {
$images = array();
while (false !== ($file = readdir($dir))) {
if ($file != "." && $file != "..") {
$images[] = $file;
asort($images);
}
}
closedir($dir);
}
And this is where I think the problem is:
foreach($images as $image) {
$subimg = substr($image, 0, -5);
if ($subimg == $poname) {
if ($x == 1) {
echo "<script type='text/javascript'>
function cycleImages(container) {
var $active = container.find('.active');
var $next = ($active.next().length > 0) ? $active.next() : container.find('img:first');
$next.css('z-index',2);
$active.fadeOut(1500,function() {
$active.css('z-index',1).show().removeClass('active');
$next.css('z-index',3).addClass('active');
});
$(document).ready(function() {
setInterval(function(){cycleImages($('#page-cycler'))}, 2000);
})
}
</script>"; ?>
<div class="cycler" id="page-cycler">
<?php $actstat = 'class="active"'; ?>
<?php $x++; ?>
<?php } else { ?>
<?php $actstat = ''; ?>
<?php } ?>
<img <?php echo $actstat; ?> src="<?php bloginfo('stylesheet_directory'); ?>/images/<?php echo $image; ?>">
<?php } ?>
<?php } ?>
</div><!-- #page-cycler -->
http://rowlandwilliams.com/lepeep/menu/
This is where your problem is:
echo "<script type='text/javascript'>
function cycleImages(container) {
var $active = container.find('.active');
var $next = ($active.next().length > 0) ? $active.next() : container.find('img:first');
$next.css('z-index',2);
$active.fadeOut(1500,function() {
$active.css('z-index',1).show().removeClass('active');
$next.css('z-index',3).addClass('active');
});
$(document).ready(function() {
setInterval(function(){cycleImages($('#page-cycler'))}, 2000);
})
}
</script>";
When you use " to echo with, php will parse all $var...
So the variable $activeand $next is being echoed from php, which doesn't look like it's set anywhere in your code. The solution would either be to change variable names from $active to active or to use single quotes ' instead of double quotes "
Using single quotes (') will tell PHP not to parse any variables inside the echo, while using double quotes (") will.
Try using ' quotes, and replace any ' quotes inside the JS with " quotes.
echo '<script type="text/javascript">
function cycleImages(container) {
var $active = container.find(".active");
var $next = ($active.next().length > 0) ? $active.next() : container.find("img:first");
$next.css("z-index",2);
$active.fadeOut(1500,function() {
$active.css("z-index",1).show().removeClass("active");
$next.css("z-index",3).addClass("active");
});
$(document).ready(function() {
setInterval(function(){cycleImages($("#page-cycler"))}, 2000);
})
}
</script>';
Your code is mess, I have rewrite your code for more clean, maybe. Since you there is no php code inside your javascript, don't try to use echo it will make your code even mess and as #Arian mentioned, $active and $next will be work as php variable.
foreach($images as $image):
$subimg = substr($image, 0, -5);
if ($subimg == $poname):
if ($x == 1): ?>
<script type='text/javascript'>
function cycleImages(container) {
var $active = container.find('.active');
var $next = ($active.next().length > 0) ? $active.next() : container.find('img:first');
$next.css('z-index',2);
$active.fadeOut(1500,function() {
$active.css('z-index',1).show().removeClass('active');
$next.css('z-index',3).addClass('active');
});
$(document).ready(function() {
setInterval(function(){cycleImages($('#page-cycler'))}, 2000);
})
}
</script>
<div class="cycler" id="page-cycler">
<?php
$actstat = 'class="active"';
$x++;
else:
$actstat = '';
endif; ?>
<img <?= $actstat; ?> src="<?php bloginfo('stylesheet_directory'); ?>/images/<?= $image; ?>">
<?php endif; ?>
<?php endforeach; ?>
</div><!-- #page-cycler -->

Categories