How to get/set session_id() or should it be generated automatically?
There are 2 ways to use sessions and session id's in PHP:
1 - Auto generate the session ID and get it:
session_start();
$id = session_id();
2 - Set the session ID manually and then start it:
session_id( 'mySessionId' );
session_start();
If you intend to set the session ID, you must set it before calling session_start();
If you intend to generate a random session_id (or continue one already started in a previous page request) and then get that id for use elsewhere, you must call session_start()
before attempting to use session_id()
to retrieve the session ID.
Start session get session id and write it into database
Problem is caused by Xampp, since some ports might be blocked, code is working with virtual machine, linux and apache server together with phpmyadmin for example. Checking the errorlogs will help to find out if the blocked ports are causing the problem.
To solve the problem together with xampp, it is recommended to deactivate all tools that might block the needed ports (skype, connection via vpn, antivirus tool, ...) or change the ports to have no conflicts there.
Usage of PHP session_id with account system
Basically, you start the session on every page. The session is initialised on the first page the user visits. A session ID is generated whenever the session is initialised, so having a session ID as a login identifier is no good. The session itself, like all PHP code, is processed server-side.
So every page should have session_start()
at the very top.
The thing you want to do is store session variables, for example $_SESSION['username']
, and check whether the user is logged in or not with something in the trend of if(!empty($_SESSION['username'])
Session die whenever you call 'session_abort()' or the browser is closed. You can also call session_set_cookie_params(3600,"/");
or something similar for a session duration (before session_start();
), where the 3600 is the number of seconds the session is active.
So one simple solution could be:
session.php
session_start();
if(!empty($_SESSION['username') {
echo "Hello";
} else {
echo $loginform;
}
randompage.php
include "session.php";
How to generate a unique session ID in php
I've achieved this system by using an OAuth type flow but we replaced the Consumer with the User.
So each domain would have the authenticated Access_Token in its own session.
You would then use that Access_Token to get information about user from an api.
I also solved the session problem using session_set_save_handler and storing sessions in a database table... This table would have the Access_Token also, making it really easy to find the session with a DB Query.
Hope this helps with ideas.
Setting Session_id in CodeIgniter
This is a hack I just suggested on another question. Place this in the __construct
of MY_Session
(untested and will not work with encryption)
public function __construct()
{
if( isset( $_GET['session_id'] ) )
$_COOKIE[ $this->sess_cookie_name ] = $_GET['session_id'];
// Session now looks up $_COOKIE[ 'session_id' ] to get the session_id
parent::__construct()
}
Related Topics
Phpunit: Doing Assertions on Non-Public Variables
Passing and Parse Paypal Ipn Custom Field
Wherejsoncontains Laravel 5.6 Not Working
PHP MySQL Set Connection Timeout
How to Fetch Account Data from a Provider with Lightopenid
Issues Porting PHP/Gd Wrapper to Imagick
Woocommerce: Display Also Product Variation Description on Cart Items
How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem
Extracting Matches from PHP Regex
How to Send a File via Axios to Laravel
How to Solve This "Warning: MySQLi_Connect(): (Hy000/1049): Unknown Database" Problem