Jump to content

php sessions


flit

Recommended Posts

Hi there,

 

I am a complete php newbie and I am trying to add lines to the end of a existing session[file] with no success. Can anyone help me add lines to an existing php session[without using a mysql database?]

Link to comment
Share on other sites

Sessions have nothing to do with a database. And, what do you mean by "add lines to the end of a existing session[file]". While the data is stored in a file on the server, the data is created/accessed as an array - you don't have any control over how the data is stored.

Link to comment
Share on other sites

Ok.

 

I created a file called page1.php with the following code:

<?php

session_start();

?>

 

<?php

$_SESSION['red']='red';

$_SESSION['blue']='blue';

print "Done";

?>

------------------------------------

I created another file called page2.php with the following code:

 

<?php

session_start();

 

echo "Roses are ".$SESSION['red'];

echo "Violets are: ".$SESSION['blue'];

?>

------------------------------------

It creates a session file with:

red|s:3:"red";blue|s:4:"blue";

 

Now I want to add another line at the end of the session file

red|s:3:"red";blue|s:4:"blue";

->new line

 

I hope you can understand my example, BTW sorry for my bad english.

Link to comment
Share on other sites

There's no need to go fiddling around with the session file. Just use the $_SESSION array

 

<?php
session_start();

echo "Roses are ".$SESSION['red'];
echo "Violets are: ".$SESSION['blue'];
$_SESSION['foo'] = 'bar'; //adds another session variable called foo with the value bar.
?>

Link to comment
Share on other sites

You could even go one step further and do this,

 

<?php

$_SESSION['colours'] = array("red" => "red", "blue" => "blue", "green" => "green");

echo "Roses are {$_SESSION['colours']['red']}, Violets are {$_SESSION['colours']['blue']}, Grass is {$_SESSION['colours']['green']}";
?>

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.