Jump to content

[SOLVED] Advice on Sessions


oceans

Recommended Posts

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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

"

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.