Jump to content

Recommended Posts

Hello everyone,

 

I have a small script that takes the screen width with screen.width from java. it-s ok to post this value on the screen... but I need to make some operation with this variable. The problem is... I don't know what kind is it? is not integer, string...

<?php

$width = "<script>document.write(screen.width);</script>";

echo $width; // it shows ok my screen width everytime.... but i cannot do operation with $width...

if ($width >= 1280) echo 'OK!';  // it will not write the OK! message....

?>

 

I tried with (int)$width ... wich will be 0, also tried intval($width) wich also returns 0;

tried this small code... wich works ok with any other string but not in my case:

 

$new_string = ereg_replace("[^0-9]", "", $width);

echo $new_string;

 

I really don't know how to convert this variable so i can work with it....

 

 

Anyone, any ideea ?

Thanks.

DR

 

Link to comment
https://forums.phpfreaks.com/topic/175151-screen-resolution-with-php/
Share on other sites

you can't do that. Firstly, you are acting as if writing javascript will return the value you wrote. Also, javascript and PHP can't talk to each other. all width has in it is a string with javascript tags and a javasript command, but the PHP can't execute the javascript. casting it as an int wont work either because its a string, and strings always get cast to 0 unless they are integral strings (IE "42", '27', etc.)

 

you can put that value into a post variable, or get variable via javascript, but you won't be able to just access that value

Php code is executed on the web server when the page is requested. The line -

 

$width = "<script>document.write(screen.width);</script>";

 

does not set $width to the screen.width value. It sets $width to that literal string containing that javascript code that once it is echoed out to the browser and executed in the browser causes the width to be displayed in the browser.

 

To get any information from the browser to the web server it must be passed as part of the HTTP request that is made for a web page. You would need to use AJAX or some other method of making the HTTP request. See the following link for an example of a "poor man's" ajax solution - http://us2.php.net/manual/en/faq.html.php#faq.html.javascript-variable

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.