×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Sessions Issue

Sessions Issue

Sessions Issue

(OP)
I know that PHP cannot fetch the screen size but using a bit of JavaScript to write a cookie, it gave me a value with which to start. However, I must have missed something because I can't get the session written! Does anyone see what's wrong? I use sessions all the time and sessions themselves are indeed being written as there are other values in use so clearly I've made a mistake that I can't spot.

CODE --> PHP

function Resolution() {
	if (isset($_SESSION['screen_width'])) :
		return $_SESSION['screen_width'];
	elseif (isset($_REQUEST['width'])) :
		$_SESSION['screen_width'] = $_REQUEST['width'];
		return $_SESSION['screen_width'];
	elseif (isset($_COOKIE['size'])) :
		$_SESSION['screen_width'] = $_COOKIE['size'];
		return $_SESSION['screen_width'];
	else :
		return "";
	endif;
} 

For reference, the JavaScript, which is working and is providing the cookie value, has simply:

CODE --> JavaScript

var c=document.cookie;
document.cookie='size='+Math.max(screen.width,screen.height)+';'; 

RE: Sessions Issue

Quote:


I can't get the session written!

Why not? What happens when it tries to write it? Do you get an error?

Where are you calling the function?

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Sessions Issue

(OP)
The "why not" is exactly my question but no, no errors but no session value either. I am callng the function in another function where the result is used to determine the font size of the logo and on another script to determine the number of columns of text the page should have.

RE: Sessions Issue

(OP)
I don't think the problem is here in this code so I'll have to look through all the other scripts and functions to see where it is. Thank you.

RE: Sessions Issue

This wouldn't necessarily apply to the logo, but you could use a CSS solution for the columns.

CODE

@media screen and (min-width: 44em) { .list{ -webkit-columns: 2; -moz-columns: 2; columns: 2; } }
@media screen and (min-width: 66em) { .list{ -webkit-columns: 3; -moz-columns: 3; columns: 3; } }
@media screen and (min-width: 88em) { .list{ -webkit-columns: 4; -moz-columns: 4; columns: 4; } }
@media screen and (min-width: 110em) { .list{ -webkit-columns: 5; -moz-columns: 5; columns: 5; } } 

With that the number of columns scales with the browser width as defined in EMs (dependent on the font-size), great for an adaptive layout.

RE: Sessions Issue

(OP)
Thank you, that's a possibility although my current column styles work a bit differently but perhaps this can be adapted to it. The logo, though, is the main issue at the moment as it is too large on lower resolution systems or mobile devices. The latter was easy to fix but the former has proven more difficult.

On the issue at hand, it turns out that if I submit it in the URL (?width=640 for example) the session does get written so it seems that the problem is in "converting" the cookie size value to the session screen_width. The cookie size value is there but the session is not being set with it. I rearranged the conditional somewhat so that I could force the cookie to change and I expected the session value to change with it but it did not.

CODE --> PHP

function Resolution() {
	if (isset($_REQUEST['width'])) :
		$_SESSION['screen_width'] = $_REQUEST['width'];
		return $_SESSION['screen_width'];
	elseif (isset($_COOKIE['size'])) :
		$_SESSION['screen_width'] = $_COOKIE['size'];
		return $_SESSION['screen_width'];
	elseif (isset($_SESSION['screen_width'])) :
		return $_SESSION['screen_width'];
	else :
		return 0;
	endif;
} 

RE: Sessions Issue

If you set the cookie using javascript, your PAGE won't see that cookie until the next page refresh. For instances like that, what I would possibly have is a special empty page just for the javascript feature detection, which then passes the values back to a php script using a location redirect (via cookie, or get parameters). That way the page refresh would happen. You might even be able to put in a meta refresh that would take effect after the javascript detection runs just in case javascript is turned off.

You could also apply a CSS solution to the logo image by having the logo being the background of an element, and then using media queries to pull the appropriate sized logo. (looking up responsive images, or responsive logos should give you plenty of information regarding that method).

RE: Sessions Issue

(OP)
Yes, I understand that so the function is being run before anything else just as you said but even so, clicking on any link should reload it. However, for testing I am reloading frequently but nothing I do seems to reset the session value. Right now the cookie size is 1280 as it should be but the session screen_width remains at the 640 that I submitted as a GET value.

As for the logo, it is being created dynamically through a PHP function so there is only one version.

RE: Sessions Issue

(OP)
I just found the problem, which goes back to vacunita's question about where I am calling the function. It turns out that it was being called within a conditional in a different function but was missing in one part. I moved it outside the conditional and as soon as I did, the session was written properly. Sorry for all the bother.

CODE --> PHP

function ScreenSize($Location) {
	Resolution();
	if (isset($_COOKIE["size"]) && $_COOKIE["size"] <= 1024) :
		$ScreenSize = "Small";
	elseif (isMobile() || Resolution() <= 1024) :
		$ScreenSize = "Small";
	elseif (isset($_SESSION['screen_width']) && $_SESSION['screen_width'] <= 1024) :
		$ScreenSize = "Small";
	else :
		$ScreenSize = "Large";
	endif;

	if ($Location == "Caption") :
		if ($ScreenSize == "Small") :
			$Size = 8;
		else :
			$Size = 10;
		endif;
	elseif ($Location = "Logo") :
		if ($ScreenSize == "Small") :
			$Size = 15;
		else :
			$Size = 18;
		endif;
	elseif ($Location = "Columns") :
		if ($ScreenSize == "Small") :
			$Size = 2;
		else :
			$Size = 3;
		endif;
	endif;
  return $Size;
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close