cant write in session. why?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

montv3
New php-forum User
New php-forum User
Posts: 7
Joined: Wed May 15, 2013 3:46 am

cant write in session. why?

Postby montv3 » Mon May 20, 2013 1:12 am

hello, when i am executing code below it seems dont work when i need to save variables to $_SESSION["username"] for example. Other pieces of this code is working. So why i cant write variables to session? i use to this code:
if(!isset($_SESSION)){
session_start();
}
in the begining to start session if its not yet been started.


if(isset($_POST["login"]))
{

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

if(isset($_POST["username"]) and $_POST["username"]!=="" and isset($_POST["password"]) and $_POST["password"]!=="")
{
$user = (User::validateUserName($_POST["username"]))?
User::getByUserName($_POST["username"]): new User();


if($user->userId and $user->password == sha1($_POST["password"]))
{

$_SESSION["acces"]=true;
$_SESSION["userId"]=$user->userId;
$_SESSION["username"]=$user->username;
header("location:index.php");
}
else
{

$_SESSION["acces"]=false;
$_SESSION["username"]=null;
header("location:401.php");
}
}
else
{

$_SESSION["acces"]=false;
$_SESSION["username"]=null;
header("location:401.php");
}

}

pbs
New php-forum User
New php-forum User
Posts: 39
Joined: Sat May 11, 2013 1:40 am
Location: Nashik, India
Contact:

Re: cant write in session. why?

Postby pbs » Mon May 20, 2013 1:25 am

Just put session_start(); at the top, don't put any condition to it.

montv3
New php-forum User
New php-forum User
Posts: 7
Joined: Wed May 15, 2013 3:46 am

Re: cant write in session. why?

Postby montv3 » Mon May 20, 2013 1:34 am

yet i need that condition, because in other piece of code when post["logout"] is called session is destroyed. of course i tried to put session_start() on top, but got same result. Still dont working. thx for answer anyway.


damn i even have these values in session file
acces|b:1;username|s:6:"montv3";userId|s:2:"37";
but when i tried to call them i got null. cant imagine why...

$GLOBALS["TEMPLATE"]["content"].=$_SESSION["username"];
$GLOBALS["TEMPLATE"]["content"].=$_SESSION["userId"];

i get
Notice: Undefined variable: _SESSION in D:\Program Files\VertrigoServ\www\svetaine\index.php on line 14
Notice: Undefined variable: _SESSION in D:\Program Files\VertrigoServ\www\svetaine\index.php on line 15

montv3
New php-forum User
New php-forum User
Posts: 7
Joined: Wed May 15, 2013 3:46 am

Re: cant write in session. why?

Postby montv3 » Mon May 20, 2013 2:05 am

SOLVED

(i got confused with included files, so essential code didnt had acces to session at all).


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 3 guests