0

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)

Mehul Kumar
  • 461
  • 8
  • 1
    You can't mix PHP and JS in that way. Why not use AJAX? – Nico Haase Feb 13 '23 at 08:07
  • 1
    Also, what do you want to achieve with that code? – Nico Haase Feb 13 '23 at 08:07
  • @NicoHaase `Get device type based on screen width` and `Using it with PHP to add custom user agent in cURL` – Mehul Kumar Feb 13 '23 at 08:08
  • Please add all clarification to your question by editing it. What keeps you from sending the value detected in the browser back to the server, eg through AJAX? – Nico Haase Feb 13 '23 at 08:12
  • @NicoHaase question updated. It simple, i need output based on rules but i am not getting it. – Mehul Kumar Feb 13 '23 at 08:15
  • you need show user-agent ? – Danz Feb 13 '23 at 08:19
  • @Danz as, i need output based on my if else condition. so, i can use this user-agent `$agent` with other PHP function like cURL, etc – Mehul Kumar Feb 13 '23 at 08:21
  • Why did you add an input type, but without a form? Why not use AJAX - why did you even add that tag, if you don't use AJAX so far? – Nico Haase Feb 13 '23 at 08:24
  • @NicoHaase there is lot of way that can be fixed. Why i did not did something because i did not know. it simple as it is. I am not profession like you. Just learning stage. So, please help me with `input` or `sample code`. That will be useful. I tried using `Ajax` but it not worked. **I have updated my question** – Mehul Kumar Feb 13 '23 at 08:26
  • "*but it not worked.*" - What do you mean with this? Did you check the developer tools for possible JS errors? Did you check the response in the console of your `ajax`-request? Why are you posting the `ajax` to the same page, do note you will only see the correct "user agent" in your (JS) console at this point and not in the `echo` part of your PHP as this is rendered *before* the `ajax` request takes place. – DarkBee Feb 13 '23 at 08:30
  • @DarkBee yes, there is no error in console. `console.log("Device type: " + device);` providing perfect output for my javascript – Mehul Kumar Feb 13 '23 at 08:33
  • You didn't answer the initial question then: What did/does not work? You are saying you are getting the correct response in the console – DarkBee Feb 13 '23 at 08:36
  • @DarkBee **Updated in Question** Hope this clear your doubt. – Mehul Kumar Feb 13 '23 at 08:39
  • 3
    It seems you still don't fully understand that `PHP` is executed/interpreted *before* it is send to a client. `JS` **can't modify** the output of `PHP`. The only advice I can give you is to actually to (re)read the question and the answers on the duplicate question. – DarkBee Feb 13 '23 at 08:42
  • @DarkBee i never said i am expert in `JS` or `php` and i am not trying to change PHP output using Javascript. i just want to read my javascript value using PHP and show the output according to it. – Mehul Kumar Feb 13 '23 at 08:47

0 Answers0