Jump to content

Is this legal/correct?


Don E

Recommended Posts

Hello everyone, Is this considered legal and/or even correct:

$_SESSION['screenWidth'] = '<script type="text/javascript">document.write(screen.availWidth);</script>';

Thanks.

Link to comment
Share on other sites

For me when I try to do some basic arithmetic for example, I don't get any results at all(as intended).

echo $result = $_SESSION['screenWidth'] + '6'; // I put 6 as a string since the type for $_SESSION is string, but even when cast them to integers, still no results as intended. echo $result = (int)$_SESSION['screenWidth'] + 6'

Edit: Yes I'm aware you can do this: echo $result = '5' + 6; //outputs 11. Because of the above not working, I thought it had to do with the type having to be equal, so that's why I had the 6 as a string: '6'

Edited by Don E
Link to comment
Share on other sites

You should be adding numbers, not strings. Use intval to convert them. But let me just make it clear that you are storing the string '<script type="text/javascript">document.write(screen.availWidth);</script>' in the session, not a number representing the width of the screen. It will convert that string to 0, and add it to 6.

Link to comment
Share on other sites

When I echo after doing this: $_SESSION['screenWidth'] = '<script type="text/javascript">document.write(screen.availWidth);</script>';, the width of my screen is displayed. Thank you for the tip on intval. I figured the string '<script type="text/javascript">document.write(screen.availWidth);</script>' is being stored there, but for some reason when echoing $_SESSION['screenWidth'], it displays the width of my screen and not the actual string.Even doing this gives me what's intended:

$_SESSION['alert'] = '<script type="text/javascript">var num = 5; alert(5);</script>';echo $_SESSION['alert'];

Link to comment
Share on other sites

That's because the string is sent to the client, and the client parses the Javascript. Look at the source code of the page on the client to see that it doesn't actually show a number.

Link to comment
Share on other sites

So it basically boils down to using Ajax to send data/info from JavaScript to PHP? I was trying to come up with a way by taking a short cut without having to use Ajax just to get that small bit of info to PHP.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...