Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session in PHP in Social Engine

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.

like image 707
KazuNino Avatar asked Jul 14 '12 08:07

KazuNino


People also ask

What is a session in PHP?

A session is a way to store information (in variables) to be used across multiple pages. Unlike a cookie, the information is not stored on the users computer.

What is PHP session and how it works?

PHP responds by sending a unique token that identifies the current session. This is known as the session ID. In all subsequent requests, the browser sends the session ID to say, "Hey, it's me again." All other data related to the session is stored on the web server. Only the session ID gets passed back and forth.

Where are PHP sessions stored?

PHP Session Start By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier). By itself, the session_start() function doesn't add much functionality to a web page.

How can store data in session in PHP?

A PHP session is used to store data on a server rather than the computer of the user. Session identifiers or SID is a unique number which is used to identify every user in a session based environment. The SID is used to link the user with his information on the server like posts, emails etc.


2 Answers

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.

like image 70
Peter Avatar answered Oct 13 '22 00:10

Peter


That's how I get my user id through session

session_start();

$userID = $viewer->getIdentity();

$_SESSION['user_id'] = $userID;

echo $_SESSION['user_id'];
like image 40
Arif Avatar answered Oct 12 '22 23:10

Arif