Jump to content

Saving data into a session


TheJoey

Recommended Posts

hi im currenlty trying to read from a file that saves all my session data into a txt file.

 

Only problem is when i read from it i only get

Resource id #3

 

<?php
$sessionfile = fopen("sessionfile.txt", "w");
fputs($sessionfile, session_encode( ) );
fclose($sessionfile)
?>

 

<?php 
session_start();

?>
<?php
$sessionfile = fopen("sessionfile.txt", "r");
session_decode(fputs($sessionfile, 4096) );

fclose($sessionfile);
?>

<html>
<body>
<?php
echo $sessionfile;
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/176352-saving-data-into-a-session/
Share on other sites

Take a look at this: http://docs.php.net/manual/en/language.types.resource.php

 

Basically, you are trying to display a file handle that is stored in $sessionfile. It is only used to identify the physical file on the hard disk after it has been opened and nothing more.

 

Anyway, your code contains more mistakes and misunderstandings of the basic PHP concepts. Take a look - you are using fputs() (writing) to read the file, whereas you should use fgets() instead. However, this is not a good function, too. You read only 4096 bytes and what if the content is longer? It will be cut off. You must either read it in a loop and then pass to the next function or use file_get_contents(). The next mistake: session_decode() does not return any decoded values, but saves them into a session. So, in order to display them, you need to use $_SESSION variable.

Trying to correct my code the first problem i ran into was

Warning: file_get_contents() expects parameter 1 to be string,

when replacing fputs.

 

The other thing i dont understand is, When you say use $_SESSION?

 

all i know is that when i do the saving part, it seems to work well and encrypts the information i need.

 

it just displaying it that im having trouble with

Once again, just take a look at the manual (it really does not hurt :)) and you'll know everything:

 

http://docs.php.net/file_get_contents

 

file_get_contents() expects string, because you put there the file name to read, not the file handle created by fopen().

 

Displaying -> when decoded, the session data are saved back to the $_SESSION array. If you need to read them or display, you have to access it, i.e.:

 

echo $_SESSION['my_decoded_variable'];
var_dump($_SESSION); // for debugging

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.