10

I have this query in mysql in a php page:

mysql_query("INSERT INTO tz_todo SET text='".$text."', 
                                     position = ".$position.", 
                                     user_id=".$_SESSION['user_id'].", 
                                     view_stat=0");

I tried to echo the query and the result is this:

INSERT INTO tz_todo SET text='trial text', position = 21, user_id=, view_stat=0

it seems that it can't get the session value of user_id.

And $_SESSION['user_id'] is not working in social engine. How to correct this? I also made a localhost version in my xampp and everything is fine but when I converted it into social engine, session is not working.

Ariful Islam
  • 7,639
  • 7
  • 36
  • 54
KazuNino
  • 149
  • 5

5 Answers5

2

In any page where you are using session objects, place this code at the beginning of the file:

if(!isset($_SESSION)){session_start();}

This way if the session is not already started, it starts it; otherwise it ignores the session start if the sesion is already started. This is important because calling session_start() if session is started already can sometimes cause errors.

Peter
  • 6,509
  • 4
  • 30
  • 34
1

That's how I get my user id through session

session_start();

$userID = $viewer->getIdentity();

$_SESSION['user_id'] = $userID;

echo $_SESSION['user_id'];
Arif
  • 1,222
  • 6
  • 29
  • 60
1

Using session to store the user_id is totally wrong. To gain a user_id try

$viewer_id = Engine_Api::_()->user()->getViewer()->getIdentity(); (or $user->getIdentity if you have another user's object).

If you still need to use session for storing this data, use Zend-approach.

Kirk Hammett
  • 656
  • 8
  • 24
0
session_start(); 
$_SESSION["test"] = "hello world";
session_start();
echo $_SESSION["test"];

does above code work ? if not, check your session.save_path in the php.ini

NOTE: to retain this variable remember to call session_start() on each php script/page before calling for the variable from the session.

Stof
  • 610
  • 7
  • 16
Sathishkumar
  • 3,394
  • 4
  • 20
  • 23
0

Yoy might be forget to start your session at the top of the page

<?php if(!isset($_SESSION)){ session_start(); } ?>

$_SESSION['user_id'] might not stored a value. check your login page (Basically after login session variables will set) or after register weather you assigned a value to that session variable..

setting a value to a session variable :

$_SESSION['user_id'] = "1234567";