-4

I'm trying to pass a value of a javascript code into a php variable, I searched a lot but I couldn't solve the whole problem.

I founded something in this site https://x10hosting.com/community/threads/check-screen-resolution-with-php.60476/#post-347641 but when I try to do a var_dump of the variable the result is a big string with a lot of spaces. I will show an example.

For example the width of the screen is 480px and I get this value in this way.

<?php $screenWidth = "<script>document.write(window.innerWidth);</script>"; ?>

But when I do a var_dump(); this is the result. string(51) "490"

A string with 51 caracteres and the true result should be int(3) "490". I tried to put the var in a trim(); bit it not worked to, how can I solve this?

Raul Rosa
  • 1
  • 1
  • Are you expecting the Javascript in that PHP variable to execute and store that value in the variable? – Enstage Sep 06 '17 at 02:02
  • Well, 51 is the length of the string "", so at least string(51) is correct. I can't imagine where 490 comes from. – deg Sep 06 '17 at 02:04
  • If the answer to my above question is yes, read here: https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming – Enstage Sep 06 '17 at 02:05
  • You can't...... – epascarello Sep 06 '17 at 02:06
  • You should have read the rest of the posts on that thread, you would have seen that that code [wouldn't work like the poster had thought it would](https://x10hosting.com/community/threads/check-screen-resolution-with-php.60476/#post-347866) – Patrick Evans Sep 06 '17 at 02:07
  • Well, the 490 result comes from "" this code where "window.innerWidth" gives me the size of the screen that is 490. – Raul Rosa Sep 06 '17 at 02:08
  • PHP runs BEFORE javascript executes. When that JavaScript runs, the page is no longer on the server. – epascarello Sep 06 '17 at 02:09
  • Possible duplicate of [How to call a JavaScript function from PHP?](https://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php) – Abdulla Nilam Sep 06 '17 at 02:14

1 Answers1

0

You would have to first have a javascript rediret page which would contain something like:

window.location = "example.com/page.php?screenwidth=" + window.innerWidth;

and then in page.php:

$screenwidth = $_GET["screenwidth"];
004123
  • 270
  • 1
  • 12
  • But I need to do the verification in the current page not in other like page.php – Raul Rosa Sep 06 '17 at 02:27
  • This is the only way of doing it, php is server side and javascript is client side. by the time the javascript runs the php is already fininshed. to do this in the current page, simply send them to a html page with this javascript code in first instead of sending them to your normal page, and then change page.php to your current page. – 004123 Sep 06 '17 at 02:32