• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

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...
0
srikanthmadishetti
Asked:
srikanthmadishetti
  • 5
  • 3
  • 2
1 Solution
 
sgtpredatorCommented:
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.
0
 
srikanthmadishettiAuthor Commented:
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;


0
 
srikanthmadishettiAuthor Commented:
<?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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
sgtpredatorCommented:
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();
0
 
star_trekCommented:

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
0
 
srikanthmadishettiAuthor Commented:
i am getting session id in 1.php page but iam not getting my session_['user'] still null
0
 
srikanthmadishettiAuthor Commented:
i have been not use cookies in this so i cant use cookies here
0
 
sgtpredatorCommented:
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?
0
 
star_trekCommented:
Try
Creating the $_SESSION variables with 0 values before writing the initial login page.  Then update the session vars with the login results and use the header() function to switch to the new location.  Once the session vars have already been created, updated values are assigned quickly.   Just be sure the new page checks both that the $_SESSION var exists AND that it's not 0.
0
 
srikanthmadishettiAuthor Commented:
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
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now