Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sessions Issue

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
1
16
US
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.

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:

JavaScript:
var c=document.cookie;
document.cookie='size='+Math.max(screen.width,screen.height)+';';
 
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
 
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.
 
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.
 
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.
 
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 [bold]size[/bold] value to the session [bold]screen_width[/bold]. The cookie [bold]size[/bold] 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.

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;
}
 
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).
 
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 [bold]size[/bold] is 1280 as it should be but the session [bold]screen_width[/bold] 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.
 
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.

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;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top