Check Screen Resolution With PHP

kbjradmin

New Member
Messages
512
Reaction score
2
Points
0
i need to check the resolution of the users screen and then change a variable based on that.
i think that his code would work, but i don't know.
could someone look it over please.

PHP:
<?php
$width = "
?>
<script type="text/javascript">
document.write = 'screen.width';
</script>
<?php
";
?>
 
thanks for your help!
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
This will give you the width:

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

You, however, can not manipulate width by manipulating that variable (and you shouldn't be changing screen sizes anyway).
 

MasterMax1313

New Member
Messages
84
Reaction score
0
Points
0
I'm not sure that will actually work, since the php is executed server side, and the javascript is executed client side. so I would think that you would need to use javascript client side to maybe use AJAX to post back to the webserver the screen size, and maybe save it in a session variable so that it is then available for the next page.
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
I'm not sure that will actually work, since the php is executed server side, and the javascript is executed client side. so I would think that you would need to use javascript client side to maybe use AJAX to post back to the webserver the screen size, and maybe save it in a session variable so that it is then available for the next page.
It will work, I tried it out.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I don't think that would work unless I'm misunderstanding what the OP wants to do. I assume he wants to get the client's resolution and make layout adjustments depending on it, such as deciding on which stylesheet to use. If this is the case, then I would personally recommend having JS get the width and height on the user's first visit. Then it should set a couple of cookies with these values and reload the page. PHP should then be able to access these values through the $_COOKIES global array.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
I don't believe he wants the resolution outputted because he said he wanted to change a variable based on the client's resolution. So I'm fairly certain he wants a way for php to get the client's resolution and use it in his script.
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
I don't believe he wants the resolution outputted because he said he wanted to change a variable based on the client's resolution. So I'm fairly certain he wants a way for php to get the client's resolution and use it in his script.
You'll have to forgive me for not seeing the difference between the two. Outputting a variable is having the information and variable. He can use the outputted information like any php variable...
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
There is a difference when JS is making the output instead of PHP. If you output JS code, the result is unknown to the server since it's only going to be executed on the client. If you set a variable to some JS code, php just sees it as a string. Try doing this with that $width variable from before:

PHP:
$width = "<script>document.write(screen.width); </script>";
if ($width > 100) {
    print 'width is greater than 100';
}

That print statement would never execute. This is what I meant by that code wouldn't work for what I believe kbjr has in mind.
 

konekt

New Member
Messages
100
Reaction score
0
Points
0
There is a difference when JS is making the output instead of PHP. If you output JS code, the result is unknown to the server since it's only going to be executed on the client. If you set a variable to some JS code, php just sees it as a string. Try doing this with that $width variable from before:

PHP:
$width = "<script>document.write(screen.width); </script>";
if ($width > 100) {
    print 'width is greater than 100';
}
That print statement would never execute. This is what I meant by that code wouldn't work for what I believe kbjr has in mind.
Yeah, you are right, I apologize. It's been a while since I've had to do something like this, and I remembered the latter attempt working.

If anyone is looking for a reason why this is, it is because the php is calling the JS which is doing the outputting. The echo statement is not doing any outputting, it's just calling on the javascript to do the writing. The $var then becomes a command almost. Thus the value of the var is the command.
 

woiwky

New Member
Messages
390
Reaction score
0
Points
0
That's understandable. I find myself having to relearn several things if I don't work with something for a while too ;-)
 

Sohail

Active Member
Messages
3,055
Reaction score
0
Points
36
Yes i think that new script should work... Couldn't you have tested it out yourself?
 
Top