Problem converting javascript variable to php

Hello,
I want to get users screen width and height and save it in mysql DB.
I tried every way suggested in search results for " convert javascript variable to php variable"

Here is another attempt:

<script>
let width = screen.width;
document.cookie = "width = " + width;
 let height = screen.height;
document.cookie = "height = " + height;
 // convert values to php variables
 
<?php
$width = "<script> document.write(width); </script>";

echo $width."<br/>";
var_dump($width);
die();

here is the code of last attempt:

<script>
let width = screen.width;
document.cookie = "width = " + width;
 let height = screen.height;
document.cookie = "height = " + height;
 // convert values to php variables
 <?php echo  "<script>document.write(width);</script>"?>
 <?php echo "<script>document.write(height);</script>"?>
</script>
<?php

$width = $_COOKIE['width'];
echo $width."<br/>";
var_dump($width);
die();

Here is a screenshot of the result

Here is another try:

<script>
let width = screen.width;
document.cookie = "width = " + width;
 let height = screen.height;
document.cookie = "height = " + height;
 // convert values to php variables
 
<?php
$width = "<script> document.write(width); </script>";

echo $width."<br/>";
var_dump($width);
die();

And here is a screenshot of the results

How do I transfer theseJavaScript variavles to php so I can insert them into a mysql DB table ?

You need to think about how the two languages work. Javascript is client side, PHP is server side.
This means everything PHP happens first, on the server, before anything goes to the client browser.
The Javascript will execute in the client browser after the data had been sent to it from the server.
PHP therefore can’t read what javascript is doing. It hasn’t happened yet, while PHP is working.
To get data from the client into your server database (via PHP) you would need javascript to make a further request to the server, sending the data along. This will require a further PHP script specifically to handle these requests.

2 Likes

insert standard “be careful how much user data you store in a database, collecting too much may put you in danger of GDPR” message.

As Sam said, PHP and Javascript operate in completely different spheres.

How do you get data from Javascript to PHP? The same way you get a text field to PHP.

1 Like

[OffTopic]
It does beg the question of: Why do you even need this info, let alone want to store it?

2 Likes

I want to know if isers use mobile or desktop devices.
I I also want to know the users location so that if I have amajority from specific locations I think I should find the reason why and do something about it.

Is it against the law to collect data about users screen device and location ?

I really can’t answer that question as I’m no lawyer, but the EU is requiring websites that if they use cookies that they need some kind of notification telling the visitor so.

For device location, sites usually ask for consent to access that. Same goes for many cookies.
For IP location, I think that’s a different case, as it can be wildly inaccurate. But I’m also not a lawyer, so don’t quote me on that.
But for your purpose, IP location may be near enough, to give a vague region.

I’m not sure screen size is still a good way to guess mobile/desktop use. The screen size of modern devices vary a lot, with some quite high res phones available. Possibly browser agent may be more reliable.

Also worth noting that both IP and User Agent can be gathered in PHP without javascript. You may need a library to get the location form the IP.
Having said that, it wouldn’t hurt you to learn a little about how the languages work and can (and can’t) work together.

Off-topic here, but I find this kind of hilarious. In order to store the answers of said notification, lol it has to be in a cookie or stored in some sort of database. Otherwise the message will always stay there forever. If you click on the “Accept disclaimer” or similar option, puts the answer in a cookie. If you click on the “No, I will not accept” or a similar option, puts the answer in a cookie. lol there’s no way around storing things in a cookie or a database. I just find that these ridiculous laws exists. What happens to the sites that aren’t created in the EU? Are they subjected to the same laws? I just find it hilarious governments are trying to regulate what the internet should and shouldn’t have.

Why though? If you’re trying to give the user the best user experience, you can use CSS for that. If you’re trying to log user information to give them ads based on what device they’re using, just letting you know that there’s add-ons that can disable Javascript so all those annoying ads won’t ever show up at all for the user. If you really want to log the data for research purposes, look into user agents.

Just for the record, while I’m not a lawyer, I’m sure it’s fine to store screen size data from visitors as well as storing IP data for determining location. If for some weird reason it’s not allowed, then it should be. I’m sure it is allowed, and this info is not cookie related.

I can be uniquely identified by the combination of my screen size and IP address. That makes it Personally Identifiable Information. You sure you want to risk storing it without my agreement? :wink:

Interesting question - meaning that I did do some extra research and it is a grey area that could be subject to privacy laws in certain jurisdictions. I wonder though if the data is not stored but only used temporarily to identify a unique visitor - just to differentiate between visitors - surely that’s not a privacy breach? Otherwise how do websites count unique visitors?

Not very accurately, I suspect. If it’s based on IP address, for example, my old company had 20+ people using standard equipment, all presenting the same external IP address. Do we count them all as the same person? That was a small business, too.

Mostly I expect the sites either use a third-party library such as Google Analytics, or just use a consistent method and compare figures after changes to the site, ad campaigns, and make assumptions.

I am not a web site traffic expert, just to confirm (if that was needed).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.