Don E Posted May 2, 2012 Share Posted May 2, 2012 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 More sharing options...
justsomeguy Posted May 2, 2012 Share Posted May 2, 2012 It's syntactically correct, you're saving a string in the session. Link to comment Share on other sites More sharing options...
Don E Posted May 2, 2012 Author Share Posted May 2, 2012 (edited) 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 May 2, 2012 by Don E Link to comment Share on other sites More sharing options...
justsomeguy Posted May 2, 2012 Share Posted May 2, 2012 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 More sharing options...
Don E Posted May 2, 2012 Author Share Posted May 2, 2012 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 More sharing options...
Ingolme Posted May 2, 2012 Share Posted May 2, 2012 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 More sharing options...
Don E Posted May 3, 2012 Author Share Posted May 3, 2012 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 More sharing options...
justsomeguy Posted May 3, 2012 Share Posted May 3, 2012 Using ajax is one way, you could also use a redirect where you load the page, figure out the dimensions, and redirect to another page with the dimensions in the querystring. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now