Jump to content

Recommended Posts

Hi all

 

I am new to and PHP. So what i am tring to do is like below

 

1. I set a variable in $_SESSION array. This variable actually is SQL Connection object.

 

2. Then i show a link on web . which redirect to other location.

like

<a href="\newaction?param=xyz">click here</a>

 

3. When now when i try to access variable in stored in $_Session in "newaction" i got nothing.. my variable is not there..

 

Please help me

 

Thanks in Adv

Link to comment
https://forums.phpfreaks.com/topic/202304-_session-geting-lost/
Share on other sites

This variable actually is SQL Connection object.

 

There are two reasons this won't work -

 

1) Resources (database connections) cannot be serialized (which is how session data is stored/retreived, and

2) All resources (no matter what variable they are stored in) used on a page are destroyed when the script on that page ends.

 

 

Rather than using the MySQL connection string in a session variable, I put my connection string in its own file and then use that file as include wherever I need it. And if I need to change the connection string, I only need to do it in one place.

 

Is there a particular reason you're putting it in a session variable? The only reason I can think of to put it in a session variable is if it's going to change quite a bit.

 

In any case, do you have session_start() in the other page? If not, it won't inherit the $_SESSION[] array.

 

If those things don't solve the problem, post some code.

 

 

what i am tring to save is mysqli object

 

like

 

$mysqli = new $mysqli(XXXXXXXX,XXXXXX,XXXXXX);

$_SESSION['con'] = $mysqli

 

<a href="/gohere.php?abc=abcc">click here </a>

...

...

 

in gohere.php

 

echo "<pre>";

print_r($_SESSION);

echo "</pre>";

 

.....

 

i get blank page ..

 

 

 

You were already told why that does not work -

This variable actually is SQL Connection object.

 

There are two reasons this won't work -

 

1) Resources (database connections) cannot be serialized (which is how session data is stored/retreived, and

2) All resources (no matter what variable they are stored in) used on a page are destroyed when the script on that page ends.

 

Even if you were on a server where persistent connections worked, you would still need to execute the code to create/get one of the persistent connections on every page load.

what i am tring to save is mysqli object

 

like

 

$mysqli = new $mysqli(XXXXXXXX,XXXXXX,XXXXXX);

$_SESSION['con'] = $mysqli

 

<a href="/gohere.php?abc=abcc">click here </a>

...

...

 

in gohere.php

 

echo "<pre>";

print_r($_SESSION);

echo "</pre>";

 

.....

 

i get blank page ..

 

 

 

 

lookup a php "include" file used as such :

 

require('my_connection_vars.php');

 

and int that script, place all your connections as follows:

 

define(DB_HOST, 'localhost');  // defined as constants or not
define(DB_NAME, 'localhost');
define(DB_USER, 'localhost');
define(DB_PASSWORD, 'localhost');
// or variables
$DB_HOST = 'LOCALHOST';
$DB_NAME = 'my_new_db';
$DB_USER = 'username';
$DB_PASSWORD = 'abc123';

 

fill in your info of course.

 

Include this file right after your session_start();

 

You can define(DB_HOST, 'localhost') as constants or not. it doesnt; really matter.  I wouldnt store that info in a session var. Session vars can be used in other ways and help to store data that has been queried from a database and you would like to reduce the number of redundant queries while the user navigates... information like "type of user"

 

Used as follows:

$_SESSION['user_type'] = $row['user_type']; 

 

Now you won't have to re-query user type on every page you need the info!  :P

 

word.

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.