Link to home
Start Free TrialLog in
Avatar of SRIKANTH MADISHETTI
SRIKANTH MADISHETTIFlag for India

asked on

sessions in php

hi
iam using php4.0 in linux my below code works in windows but in linux i am having problem to access the session variable after redirecting.

if(username=username && password== password)
{
$_SESSION['user'] = $username;
            session_write_close();  
            header("Location:1.php");
            }else{
            header("Location:login.php?msg=pwdErr");
}
any ideas please...
Avatar of sgtpredator
sgtpredator

first are you sure this is correct:  if(username=username && password== password)

shouldn't there be a == between usernames?

second are you sure you are starting the session on the all pages using:  session_start();

Also you should be retrieving the session variable in 1.php like this:  $username=$_SESSION['user'];

Currently the if statment is always going to return true becuase you have = instead of == which means the else statement will never get processed.  I hope that helps.
Avatar of SRIKANTH MADISHETTI

ASKER

hey that if loop i was giving for an example any way iam taking username and password from database

there is no problem in it i just used here for an idea

but my main problem is in my next page i am not getting this session variable value.

i even used session_write_close();  because as iam redirecting i used session_write_close

but still iam getting $_SESSION['user'] value null.

and see iam assigning the value username which user entered and after checking it with the data base iam assigning it to $_SESSION['user']
so i think i should compulsory use
$_SESSION['user'] = $username;


<?php
session_start();

$msg="";

if(isset($_POST['username']))
{

$username = $_POST['username'];
$password = $_POST['userpass'];

      if($password == "dss" && $username == "dss")
{            
            $_SESSION['user'] = $username;
            session_write_close();  
            header("Location:1.php");
            }else{
            header("Location:login.php?msg=pwdErr");
      }
}
?>

this is my code but when i go to 1.php i get $_SESSION['user'] value null.
Sorry I thought that was actual code.  

Have you tried passing the session id through the url.  If you don't pass the SID through the URL it requires cookies.  I am not sure how the cookies are handled in linux.  You can however pass the sid using the URL and you never have to worry about your site not working becuase of disabled cookies.

header("location:1.php?" . SID );
exit();

The code above attaches the session id to the url and redirects the browser then exit halts all parsing of code.  Now you should be able to retrieve your session variable in 1.php as long as you have started the session using session_start();

You can pass sessions variables using get or cookies

For get you have pass it in the URL on each page

for cookies, you can use Cookie function in PHP, you have return cookies before the <html> page. The explanation of Cookie function can be found at
http://us2.php.net/manual/en/function.setcookie.php
i am getting session id in 1.php page but iam not getting my session_['user'] still null
i have been not use cookies in this so i cant use cookies here
Can you add the exit() function  after your header on the main page and try it again?  Also if that does not work can you show me the code block from 1.php?
ASKER CERTIFIED SOLUTION
Avatar of star_trek
star_trek

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks that was real gr8 help i just assigned $_session['user'] to 0 first then i was updating with username entered it was done

thanks star_trek

and thank you to sgtpredator