oceans Posted April 19, 2007 Share Posted April 19, 2007 Please guide on how to use Sessions. (I noted it is different from ASP) I came to know we should always declare Session before <HTML> Like this <?php session_start(); // store session data $_SESSION['views']=1; ?> <html> <body> <?php //retrieve session data echo "Pageviews=". $_SESSION['views']; ?> </body> </html> What if we need to store and transfer data while we are working within a form? Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 Dear People, I tried all "patterns" I am not getting there. My intention; while a form is being worked in a push of a button, some values will be stored on session variable, than at the next page this values will be retrieved. In ASP, we use "Session("01LogInTO03Maintain" & I)=rs(I)" to assign a value; InPutFromNToScreen(I)=Session("01LogInTO03Maintain" & I) to retrieve. Storage and Retrival can be done any where. Please help Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 Try this: $_SESSION['views']='1'; Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks, When I used "echo $_SESSION['views'];" on the second page I got " PHP Notice: Undefined variable: _SESSION in C:\Inetpub\wwwroot\MyPHP\Member\0301SignUp.php on line 21" on the second page Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 Oh these are 2 pages: Then try this one... <?php session_start(); // store session data $_SESSION['views']='1'; ?> <html> <body> <?php session_start(); $page=$_SESSION['views']; //retrieve session data echo "Pageviews= $page"; ?> </body> </html> Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 Sorry I was away, OK may I clerify my needs: Page 1; some where in middle of a form I set a session veriable Page 2; some where in middle of a form I use it Now here you have set and assigned even before a page start , please explain thanks. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 1st of all in php session are put at the very top of the page, after php tag like: <?php session_start(); //Do ur stuff here ?> Note: Also before html i mean if u want to use forms then also u have put it on the top. If u want an alternative of this then use like this.. <?php ob_start(); //now u can use session anywhere in ur page. ?> U have to set sessions like this in php. On every page u want to use session variable u have to set: session_start(); Hope it will help u. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 I hope I am not bugging you :'( I will give you my situation: I will verify password then keep the user ID for further use for next pages. Can you show(simple example) how to send from page 1 and receive in page 2. sorry Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 Hi, Session_Start will initialise the session or if one exists indicate to the server that sessions and session variables are required. So on every page that will either use or set session variables include the session_start at the head of the page. I would then use the isset() function to check for existence of the variable you are using/want to use. For example - here I am checking for a shopping cart I may have set earlier (or not) ! <code> session_start(); if (!isset($_SESSION['cart'])) { header("Location:index.php"); } </code> In "the middle" of my html I might do something like <code> <? $_SESSION['cart'] = 'widget'; ?> </code> The cart variable is then set and is available for the duration of the session. Is this any good for you ? CyrilSnod Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 I didi not understand this cyrilsnodgrass <code> </code> Other the example is ok. Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 ignore <code> </code> I am screwing up my posting somehow.......I am a relative noobster after all ! Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 Thanks, I followed your advice; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <?PHP session_start(); $_SESSION['views']=0; ?><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> bla bla bla <?PHP //Checking for Unfilled Fields $TxtBoxesNotFullyFilled=0; for ($i=1; $i<=2; $i++) { if ($Input[$i]=="") { $TxtBoxesNotFullyFilled=1; break; } } if ($TxtBoxesNotFullyFilled==1) { echo "All Fields Should be Filled !"; } else { /*$con = mysql_connect("localhost","peter","abc123"); if ($con) { } else { die('Could not connect: ' . mysql_error()); } */ $_SESSION['views']=1; header("location: 0301SignUp.php"); } ?> all on the same page , I got the following error; " PHP Warning: session_start() [function.session-start]: open(C:\DOCUME~1\zubir\LOCALS~1\Temp\php\session\sess_2hju7umqrgu32g3ut1c3u76vv7, O_RDWR) failed: Permission denied (13) in C:\Inetpub\wwwroot\MyPHP\Member\0101LogIn.php on line 3 PHP Warning: Unknown: open(C:\DOCUME~1\zubir\LOCALS~1\Temp\php\session\sess_2hju7umqrgu32g3ut1c3u76vv7, O_RDWR) failed: Permission denied (13) in Unknown on line 0 PHP Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\DOCUME~1\zubir\LOCALS~1\Temp\php\session) in Unknown on line 0 " Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 Hi, This looks like something to do with the session.save_path variable in your php.ini file - anyone ? Certainly now seems to be trying to generate & save the sessionid Quote Link to comment Share on other sites More sharing options...
bennyboywonder Posted April 19, 2007 Share Posted April 19, 2007 Hey. I don't know if I am using a different version of php to you, but I always use session variables in a different way <?php session_start(); session_register('myvar'); this registers the normal php variable $myvar as a session variable. The value will be avaliable to all pages, so you can set it like this <?php $myvar = "loggedin"; ?> and test for it like this <?php if($myvar) { echo "user is logged in! YAY!"; } ?> Hope this helps Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 session_register is used in php 4.2.3 or lower versions. $_SESSION['']; in new latest versions. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 I am using the ver 5, I noted that the C:/..../php/session does nerver get filled at any time. i believe it is more to setting any help for me. Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted April 19, 2007 Share Posted April 19, 2007 Chack ur php session path. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 seems too technical can any one guide me Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 I think you need to find and edit you php.ini file. You are looking for an entry that looks like session.save_path= <pathname> where pathname is supposed to point to an existing directory on your server - with appropriate permissions to allow PHP to use that directory. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 You won't believe me I do not have this file what shell I do, installation was pretty easy Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 sorry i am too quick, please guide me step by step thanks Quote Link to comment Share on other sites More sharing options...
cyrilsnodgrass Posted April 19, 2007 Share Posted April 19, 2007 it is on your server somewhere it would have been set up during installation. it may have been called something slightly different depending on where you got the product from - php.ini-dist for example ?? Quote Link to comment Share on other sites More sharing options...
oceans Posted April 19, 2007 Author Share Posted April 19, 2007 i found the file in my computer, what sentence should i look for Quote Link to comment Share on other sites More sharing options...
oceans Posted April 20, 2007 Author Share Posted April 20, 2007 Dear People, Can any one help me, I am getting no where, Thanks. Quote Link to comment Share on other sites More sharing options...
oceans Posted April 20, 2007 Author Share Posted April 20, 2007 People, this thing suppose to be simple but it is difficult, can any one help please :'( Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.