Jump to content

PHP Object store in session


ttommy

Recommended Posts

Hi,
I create a site with php oop. I want to user to login and store the curently loged in user information into an oject call user. How do I pass this object to the other pages? Or I just have to pass the primary key to other page and use new object to retrieve its information? Will it be too much query for each page? Can I reduce the amount of query to database in some other way?
Thanks for ur reply
Link to comment
Share on other sites

Here is a session example using a class I wrote called Login.
For your needs create a class called user with all the information you would like to store about them and just follow the example I have given you. This is far better than calling the information from the database on each subsequent page.

Another usefull tip is to modify your session.save_path in your php.ini file that way when the session is created you can open it in a local editor like notepad(if you are running php and testing the website you are creating on your local machine). I have modified mine to look like so session.save_path = "C:/Php5/sessions". As soon as the session is created i.e the user loads the page index.php if you go to that path you will check the session sitting there it will look something like this sess_2dc08acbc640cd5969383bb103b8e739 now when you capture the information on the form and move to the next page you will notice the session file changes size and stores the info you requested it to.

index.php
[quote]
<?php
//Declare class Login
require_once("clsLogin.inc.php");
//Start a session so we can transferr the information across pages
session_start();
//Check if the form was submitted
if ($_POST['cmdLogin'] == "Login"){
    //Create a Login Object with the information obtained from the form
$login = new Login($_POST['txtUsername'],$_POST['txtPassword']);
    //Register the session and store the Login object inside it
$_SESSION['LoginObject'] = $login;
    //Forward them to page 2
header("Location:page2.php");
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>
<body>
<form id="frmLogin" name="frmLogin" method="post" action="<?php echo $_Server['PHP_SELF'] ?>">
  <table width="200">
    <tr>
      <td><label for="label">Username:</label></td>
      <td><input type="text" name="txtUsername" id="txtUsername" /></td>
    </tr>
    <tr>
      <td><label for="label2">Password:</label></td>
      <td><input type="password" name="txtPassword" id="label" /></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input name="cmdLogin" type="submit" id="cmdLogin" value="Login" /></td>
    </tr>
  </table>
</form>
</body>
</html>
[/quote]

page2.php
[quote]
<?php
//Declare class Login
require_once("clsLogin.inc.php");
//Always start the session so the page knows there is a session object
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<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>

<body>
<?php
//Declare a variable equal to the session object Login
$login = $_SESSION['LoginObject'];
//Now we have associated the class with the session object we can call the class methods on our variable
echo "Username: " .$login->getLoginUsername();
echo "<br />";
echo "Password: " .$login->getLoginPassword();
?>
</body>
</html>

[/quote]

clsLogin.php
[quote]
<?PHP
//Declare class object and its methods
class login
{
var $username;
var $password;

function Login($aUsername,$aPassword){
$this->username = $aUsername;
$this->password = $aPassword;
}
function getLoginUsername(){
return $this->username;
}
function getLoginPassword(){
return $this->password;
}
}
?>
[/quote]

This is quite a long example  8) if you need more help just ask

Cheers
Link to comment
Share on other sites

Thankyou very much I'm very clear about this by now. However, I have read some article said that if use an object to a session, I have to pass it through a function call serialize() and when get the object from session I have to unserialize() the session variable. What is the diff between your method and the one mentioned above?
Thank for you reply
Link to comment
Share on other sites

you only have to call Serialize and Unserialise if you want to store the information into a file or a database. Serialize() returns a string containing a byte-stream representation of any value that can be stored in PHP. unserialize() can use this string to recreate the original variable values. If you using php sessions to store the info accross the pages its not nessesary to perform these steps. However if you want to store the information to a file or databse then this becomes necessary

Hope this helps :P
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.