note 11778 deleted from function.session-start by jimw

From: Date: Fri, 07 Dec 2001 20:25:55 +0000
Subject: note 11778 deleted from function.session-start by jimw
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-21903@lists.php.net to get a copy of this message
I found some users had problems with sessions, i think most of them benefited from the notes i added. Here i'am adding a detailed example here..and simple ---------------------------------------- Prerequisites: PHP4.x must be installed. Your web server must be configured to recognize whatever PHP file extension you give to this script (.php, .phtml, etc.) ---------------------------------------- <? session_start(); //simply starting a session $msg = "started a session...."; ?> --------------------------------------------------------- Simple access counting sessions.. ------------------------------------------------- <? session_start(); //start a sessiion session_register('count'); //register variable count $count++; $msg = "<P>You've been here $count times. Thanks!</p>"; ?> ----------------------------------- Two-step form that uses sessions to track user preferences. session01.php ------------------------------------------------------------- <? session_start(); if (!$PHPSESSID) { session_register('font_family'); session_register('font_size'); } else if ((!$font_family) || (!$font_size)) { session_register('font_family'); session_register('font_size'); } if (!$font_family) { $font_family = "sans-serif"; } if (!$font_size) { $font_size = "10"; } ?> <HTML> <HEAD> <TITLE>My Display Preferences</TITLE> <style type="text/css"> BODY, P, A {font-family:<? echo "$font_family"; ?>;font-size:<? echo "$font_size"; ?>pt;font-weight:normal;} H1 {font-family:<? echo "$font_family"; ?>;font-size:<? echo $font_size + 4; ?>pt;font-weight:bold;} </style> </HEAD> <BODY> <H1>Set Your Display Preferences</H1> <FORM METHOD="POST" ACTION="session02.php"> <P>Pick a Font Family:<br> <input type="radio" name="sel_font_family" value="serif"> serif <input type="radio" name="sel_font_family" value="sans-serif" checked> sans-serif <input type="radio" name="sel_font_family" value="Courier"> Courier <input type="radio" name="sel_font_family" value="Wingdings"> Wingdings </p> <P>Pick a Base Font Size:<br> <input type="radio" name="sel_font_size" value="8"> 8pt <input type="radio" name="sel_font_size" value="10" checked> 10pt <input type="radio" name="sel_font_size" value="12"> 12pt <input type="radio" name="sel_font_size" value="14"> 14pt </p> <P><input type="submit" name="submit" value="Set Display Preferences"></p> </FORM> </BODY> </HTML> --------------------------------------------------- session02.php -------------------------------------------------- <? session_start(); if (!$PHPSESSID) { session_register('font_family'); session_register('font_size'); } else if ((!$font_family) || (!$font_size)) { session_register('font_family'); session_register('font_size'); } $font_family = $sel_font_family; $font_size = $sel_font_size; if (!$font_family) { $font_family = "sans-serif"; } if (!$font_size) { $font_size = "10"; } ?> <HTML> <HEAD> <TITLE>My Display Preferences</TITLE> <STYLE type="text/css"> BODY, P, A {font-family:<? echo "$font_family"; ?>;font-size:<? echo "$font_size"; ?>pt;font-weight:normal;} H1 {font-family:<? echo "$font_family"; ?>;font-size:<? echo $font_size + 4; ?>pt;font-weight:bold;} </STYLE> </HEAD> <H1>Your Preferences Have Been Set</H1> <P>As you can see, your selected font family is now <? echo "$font_family"; ?>, with a base size of <? echo "$font_size" ?> pt.</p> <P>Please feel free to <a href="session01.php">change your preferences</a> again.</p> </BODY> </HTML> ---------------------------------------- Happy coding :)

« previous php.notes (#21903) next »