i am using Javascript to get screen size and using with PHP to rotate user-agent based on device type.
test.php code :
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
var width = window.innerWidth;
var device = "";
if (width <= 480) {
device = "mobile";
} else if (width <= 650) {
device = "tablet";
} else {
device = "desktop";
}
//console.log("Device type: " + device);
$.ajax({
url: 'test.php',
type: 'POST',
data: {device: device},
success: function(response) {
console.log(response);
}
});
</script>
<?php
$device = $_POST['device'];
if ($device == "desktop") {
$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/109.0';
} else if ($device == "mobile") {
$agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1';
} else if ($device == "tablet") {
$agent = 'Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1';
} else {
$agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36';
}
echo "Device Type" .$device;
echo "User Agent: " . $agent;
?>
$agent
in PHP : I am always getting else
part as output even when i am trying with different screen width size like Mobile, Tablet and Desktop.
$device
in PHP - Not getting the values passed by device
in Javascript
What i want : I want $agent
to show or use values depend upon device size from my PHP if else
condition. So i can use $agent
in other PHP Function (for example cURL
)