php session variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jack

    php session variable

    Hi everyone,
    I am stuck on something really stupid and simple. Here is what I am
    trying to do. Login, check the username and password against db and if
    it checks out then check the logged variable session and pass the user
    to the next page. Here is a sample of the login process page:

    $user = 'test';
    $pass = sha1('testpass' );
    //set the database connection variables
    $hostname = "localhost" ;
    $dbusername = "newuser";
    $dbpassword = "newpasswor d";
    $dbh = mysql_connect($ hostname, $dbusername, $dbpassword) or
    die("Unable to connect to the database");
    mysql_select_db ("seandb", $dbh);
    $result = mysql_query("se lect * from users where username='$user ' AND
    password='$pass '", $dbh);

    //check that at least one row was returned

    $rowCheck = mysql_num_rows( $result);
    if($rowCheck 0){
    while($row = mysql_fetch_arr ay($result)){

    //start the session
    session_start() ;
    session_registe r('sname');
    $sname = $user;
    //successful login code will go here...
    header( "Location: main.php" );
    }
    }else {
    include 'index.php';
    printf("Incorre ct Login...");
    }

    Then on the Main page I want to be able check the variable sname and
    if it is set then say hi.

    For some reason this doesn't happen. The variable gets assigned a
    variable but once it is passed over to main.php, the session variable
    is not there anymore. Here is the code for main.php:
    if (!empty($_SESSI ON['sname'])) {
    ?>
    <p>Welcome</p><?php echo($_SESSION['sname']);?>

    <?php
    }else{
    printf("failed. ..");
    //header( "Location: index.php" );
    }

    For some reason I always get the "failed" message

    any ideas...?

    Thanks

    J

  • ZeldorBlat

    #2
    Re: php session variable

    On Jul 3, 6:07 pm, Jack <accpac...@hotm ail.comwrote:
    Hi everyone,
    I am stuck on something really stupid and simple. Here is what I am
    trying to do. Login, check the username and password against db and if
    it checks out then check the logged variable session and pass the user
    to the next page. Here is a sample of the login process page:
    >
    $user = 'test';
    $pass = sha1('testpass' );
    //set the database connection variables
    $hostname = "localhost" ;
    $dbusername = "newuser";
    $dbpassword = "newpasswor d";
    $dbh = mysql_connect($ hostname, $dbusername, $dbpassword) or
    die("Unable to connect to the database");
    mysql_select_db ("seandb", $dbh);
    $result = mysql_query("se lect * from users where username='$user ' AND
    password='$pass '", $dbh);
    >
    //check that at least one row was returned
    >
    $rowCheck = mysql_num_rows( $result);
    if($rowCheck 0){
    while($row = mysql_fetch_arr ay($result)){
    >
    //start the session
    session_start() ;
    session_registe r('sname');
    $sname = $user;
    //successful login code will go here...
    header( "Location: main.php" );
    }}else {
    >
    include 'index.php';
    printf("Incorre ct Login...");
    >
    }
    >
    Then on the Main page I want to be able check the variable sname and
    if it is set then say hi.
    >
    For some reason this doesn't happen. The variable gets assigned a
    variable but once it is passed over to main.php, the session variable
    is not there anymore. Here is the code for main.php:
    if (!empty($_SESSI ON['sname'])) {
    ?>
    <p>Welcome</p><?php echo($_SESSION['sname']);?>
    >
    <?php}else{
    >
    printf("failed. ..");
    //header( "Location: index.php" );
    >
    }
    >
    For some reason I always get the "failed" message
    >
    any ideas...?
    >
    Thanks
    >
    J
    Two things:

    1. Don't use session_registe r. Just put it in the session array:
    $_SESSION['sname'] = $user;

    2. On main.php are you calling session_start() before you do
    anything? If you aren't, you should be.

    Comment

    • Jack

      #3
      Re: php session variable

      On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
      On Jul 3, 6:07 pm, Jack <accpac...@hotm ail.comwrote:
      >
      >
      >
      Hi everyone,
      I am stuck on something really stupid and simple. Here is what I am
      trying to do. Login, check the username and password against db and if
      it checks out then check the logged variable session and pass the user
      to the next page. Here is a sample of the login process page:
      >
      $user = 'test';
      $pass = sha1('testpass' );
      //set the database connection variables
      $hostname = "localhost" ;
      $dbusername = "newuser";
      $dbpassword = "newpasswor d";
      $dbh = mysql_connect($ hostname, $dbusername, $dbpassword) or
      die("Unable to connect to the database");
      mysql_select_db ("seandb", $dbh);
      $result = mysql_query("se lect * from users where username='$user ' AND
      password='$pass '", $dbh);
      >
      //check that at least one row was returned
      >
      $rowCheck = mysql_num_rows( $result);
      if($rowCheck 0){
      while($row = mysql_fetch_arr ay($result)){
      >
      //start the session
      session_start() ;
      session_registe r('sname');
      $sname = $user;
      //successful login code will go here...
      header( "Location: main.php" );
      }}else {
      >
      include 'index.php';
      printf("Incorre ct Login...");
      >
      }
      >
      Then on the Main page I want to be able check the variable sname and
      if it is set then say hi.
      >
      For some reason this doesn't happen. The variable gets assigned a
      variable but once it is passed over to main.php, the session variable
      is not there anymore. Here is the code for main.php:
      if (!empty($_SESSI ON['sname'])) {
      ?>
      <p>Welcome</p><?php echo($_SESSION['sname']);?>
      >
      <?php}else{
      >
      printf("failed. ..");
      //header( "Location: index.php" );
      >
      }
      >
      For some reason I always get the "failed" message
      >
      any ideas...?
      >
      Thanks
      >
      J
      >
      Two things:
      >
      1. Don't use session_registe r. Just put it in the session array:
      $_SESSION['sname'] = $user;
      >
      2. On main.php are you calling session_start() before you do
      anything? If you aren't, you should be.
      I made the changes but still no cigar.
      login.php
      ---------
      while($row = mysql_fetch_arr ay($result)){

      //start the session
      session_start() ;
      $_SESSION['sname'] = $user;
      //successful login code will go here...
      header( "Location: main.php" );
      }
      main.php
      ---------
      session_start() ;
      if (!empty($_SESSI ON['sname'])) {
      ?>
      <p>Welcome</p>

      <?php
      }
      ?>

      Comment

      • ZeldorBlat

        #4
        Re: php session variable

        On Jul 3, 6:17 pm, Jack <accpac...@hotm ail.comwrote:
        On Jul 3, 3:11 pm, ZeldorBlat <zeldorb...@gma il.comwrote:
        >
        >
        >
        On Jul 3, 6:07 pm, Jack <accpac...@hotm ail.comwrote:
        >
        Hi everyone,
        I am stuck on something really stupid and simple. Here is what I am
        trying to do. Login, check the username and password against db and if
        it checks out then check the logged variable session and pass the user
        to the next page. Here is a sample of the login process page:
        >
        $user = 'test';
        $pass = sha1('testpass' );
        //set the database connection variables
        $hostname = "localhost" ;
        $dbusername = "newuser";
        $dbpassword = "newpasswor d";
        $dbh = mysql_connect($ hostname, $dbusername, $dbpassword) or
        die("Unable to connect to the database");
        mysql_select_db ("seandb", $dbh);
        $result = mysql_query("se lect * from users where username='$user ' AND
        password='$pass '", $dbh);
        >
        //check that at least one row was returned
        >
        $rowCheck = mysql_num_rows( $result);
        if($rowCheck 0){
        while($row = mysql_fetch_arr ay($result)){
        >
        //start the session
        session_start() ;
        session_registe r('sname');
        $sname = $user;
        //successful login code will go here...
        header( "Location: main.php" );
        }}else {
        >
        include 'index.php';
        printf("Incorre ct Login...");
        >
        }
        >
        Then on the Main page I want to be able check the variable sname and
        if it is set then say hi.
        >
        For some reason this doesn't happen. The variable gets assigned a
        variable but once it is passed over to main.php, the session variable
        is not there anymore. Here is the code for main.php:
        if (!empty($_SESSI ON['sname'])) {
        ?>
        <p>Welcome</p><?php echo($_SESSION['sname']);?>
        >
        <?php}else{
        >
        printf("failed. ..");
        //header( "Location: index.php" );
        >
        }
        >
        For some reason I always get the "failed" message
        >
        any ideas...?
        >
        Thanks
        >
        J
        >
        Two things:
        >
        1. Don't use session_registe r. Just put it in the session array:
        $_SESSION['sname'] = $user;
        >
        2. On main.php are you calling session_start() before you do
        anything? If you aren't, you should be.
        >
        I made the changes but still no cigar.
        login.php
        ---------
        while($row = mysql_fetch_arr ay($result)){
        >
        //start the session
        session_start() ;
        $_SESSION['sname'] = $user;
        //successful login code will go here...
        header( "Location: main.php" );
        }
        main.php
        ---------
        session_start() ;
        if (!empty($_SESSI ON['sname'])) {
        ?>
        <p>Welcome</p>
        >
        <?php}
        >
        ?>
        Make sure you have cookies enabled. Also make sure error_reporting
        and display_errors is enabled so you can see if there are any problems.

        Comment

        Working...