codesuck codesuck - 10 months ago 39
PHP Question

Session is not starting in my sub domain php

I have tried every method to start a session in sub domain but is not working

Here is sample of my php login code

$username = $_POST['login'];

//Database Query
session_set_cookie_params(0, "/", "", false, false);
$_SESSION['username'] = $username;
$_SESSION['ini'] = true;

Then I use this on top of every page in main domain


This on i put this at the top of every file

session_set_cookie_params(0, "/", "", false, false);

But still is not working can someone tell me what to do?

Lastly i make this function and session is working in sub-domain but out in main domain

I added below function at the top of login script

function new_session_start(){
$secure = false;
// This stops JavaScript being able to access the session id.
$httponly = false;
// Gets current cookies params. //$cookieParams["domain"],$cookieParams["lifetime"]
$cookieParams = session_get_cookie_params();
session_start(); // Start the PHP session
session_regenerate_id(true); // regenerated the session, delete the old one.

Then use this at the top of subdomain page

session_set_cookie_params(0, "/", "", false, false);

Answer Source

The problem here is not exactly the same as other poeple are thinking. On other questions they are talking about how to create a session for subdomain. It looks like you did everything correct as told on other questions. The problem here is in the session_set_cookie_params(0, "/", "", false, false).

You didn't add this code to your main page so the php will make that cookie by himself based on your domain. Your domain is so your php will do it like session_set_cookie_params(0, "/", "", false, false).

Then when you go to your subdomain he is searching for but he won't find it because the cookie of your main domain is and not

How to fix it: Add: session_set_cookie_params(0, "/", "", false, false); to your main page to so he will create the right cookie.