Sessions not working in subsequent pages

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • urfriend202
    New Member
    • Nov 2008
    • 9

    Sessions not working in subsequent pages

    Hi,

    I am developing a website. In Login form , on successful login of user, i had created session variable $_SESSION['username'] and redirected to second page. In second page when i checked , isset($_SESION['username']) is returning false.

    This is the first website I am developing. Do I have to change any settings in php.ini for sessions to work?
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by urfriend202
    Hi,

    I am developing a website. In Login form , on successful login of user, i had created session variable $_SESSION['username'] and redirected to second page. In second page when i checked , isset($_SESION['username']) is returning false.

    This is the first website I am developing. Do I have to change any settings in php.ini for sessions to work?
    For us to *really* help and not just guess, we need to see your code. Please post it using [code] tags.

    My guess is you're not starting the session with session_start() .

    Comment

    • urfriend202
      New Member
      • Nov 2008
      • 9

      #3
      Here is the code:

      loginpage.php


      Code:
      <?php
      session_start();
      require('config.php');
      
      if($_POST['submit'])
      {
        $username = $_POST['name'];
        $password = $_POST['password'];
        $mysql = mysql_connect($dbhost,$dbuser,$dbpassword);
        mysql_select_db($dbdatabase,$mysql);
        $loginsql = "select * from logins where username='" . $username ."' and password='". $password. "';";
        $loginres = mysql_query($loginsql);
        $numlogin = mysql_num_rows($loginres);
        $row = mysql_fetch_assoc($loginres);
        if($numlogin==1)
         {
          session_register('USERNAME');
          session_register('SESS_LOGGEDIN');
          $_SESSION['USERNAME'] = $row['username'];
          $_SESSION['SESS_LOGGEDIN'] = 1;
          if(isset($_SESSION['USERNAME']))
           {
             header("Location:".$config_basedir."index.php");
      
           }
            else echo"sessions not set";
         }
         else
           echo "Incorrect Login, please try again";
      
      
      
      }
      require('header.php');
      
      
      
      ?>
      
      <form name='loginForm' action='<?php $SCRIPT_NAME?>' method='POST'>
      
      <table>
      <tr>
      <td> UserName : </td> <td><input type='text' name='name'></td>
      </tr>
      <tr>
      <td> Password : </td> <td><input type='password' name='password'></td>
      </tr>
      <tr>
      <td> </td> <td><input type='submit' name='submit' value='Login'></td>
      </td>
      </table>
      
      </form>
      
      
      
      
      <?php
      require('footer.php');
      ?>
      On successfull login of user , page is redirected to index.php which is using header.php

      index.php

      Code:
      <?php
      
       require('header.php');
      ?>

      header.php


      Code:
      <?php
      session_start();
       require('config.php');
       $db = mysql_connect($dbhost,$dbuser,$dbpassword);
       mysql_select_db($dbdatabase,$db);
      ?>
      
      <html>
      
      <head>
      <title><?php echo $config_blogname; ?> </title>
      <link rel='stylesheet' href='stylesheet.css' tyep='text/css'>
      </head>
      
      <body>
      <div id='header'>
      <h1><?php echo $config_blogname; ?></h1>
      </div>
      <div id='menu'>
      <a href='index.php'>Home</a>
      <a href = 'viewcat.php'> Categories </a>
      <?php
      if(isset($_SESSION['USERNAME']))
      {
         echo "<a href='logout.php'> Logout</a>".$_SESSION['USERNAME'];
         echo "<a href='addcat.php'> Add Category </a>";
         echo "<a href='addentry.php'> Add Entry </a>";
      }
      else
        echo "<a href='loginpage.php'> Login </a> <a href='signup.php'> SignUp </a> ";
      
      ?>
      </div>
      <div id="bar">
      <img src= "../shoppingcart/Sai.jpg" width="200px" height="250px" top="200px">
      </div>
      <div id='main'>
      In header.php isset($_SESSION['USERNAME']) is returning false.
      Last edited by Markus; Nov 26 '08, 11:15 AM. Reason: fixed code tags

      Comment

      • maheswaran
        New Member
        • Mar 2007
        • 190

        #4
        have u check in loginpage line 16 $row['username']; is return any thing?

        Comment

        • urfriend202
          New Member
          • Nov 2008
          • 9

          #5
          Yes. It is returning username correctly. In loginpage
          isset($_SESSION['USERNAME']) is returning true. But echo $_SESSION['USERNAME'] is not printing anything.

          Comment

          • jrhitokiri
            New Member
            • Apr 2008
            • 35

            #6
            try changing your code in the index.php to:

            Code:
            <?php
                      session_start();
                      require('header.php');
            ?>
            I hope this helps!

            Comment

            • Markus
              Recognized Expert Expert
              • Jun 2007
              • 6092

              #7
              Originally posted by jrhitokiri
              try changing your code in the index.php to:

              Code:
              <?php
                        session_start();
                        require('header.php');
              ?>
              I hope this helps!
              That shouldn't change anything but give you a notice for starting the session again.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Take lines 17 and 18 out of loginpage.php - this won't be the problem, but you don't need to use them.

                I can't see anything wrong with what you posted.

                We'll have to see if someone else can spot it.

                Comment

                • urfriend202
                  New Member
                  • Nov 2008
                  • 9

                  #9
                  Originally posted by jrhitokiri
                  try changing your code in the index.php to:

                  Code:
                  <?php
                            session_start();
                            require('header.php');
                  ?>
                  I hope this helps!
                  I tried this. Its working. Thanks. It helped me continue two of my stucked projects. Thank you sooo much

                  Comment

                  • urfriend202
                    New Member
                    • Nov 2008
                    • 9

                    #10
                    Sessions are working fine in Internet Explorer. But not in Mozilla Firefox . Can anyone tell me why is that and what should I do for that to work?

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by urfriend202
                      I tried this. Its working. Thanks. It helped me continue two of my stucked projects. Thank you sooo much
                      I'll be damned..

                      Comment

                      • jrhitokiri
                        New Member
                        • Apr 2008
                        • 35

                        #12
                        Originally posted by urfriend202
                        I tried this. Its working. Thanks. It helped me continue two of my stucked projects. Thank you sooo much
                        glad i could be of help.. ^^,

                        and for your next problem, what happens in IE that does not happen in mozilla..? can you expound on the problem..?

                        Originally posted by Markus
                        I'll be damned..
                        ahahaha! XD lucky strike..?XD

                        Comment

                        • urfriend202
                          New Member
                          • Nov 2008
                          • 9

                          #13
                          Originally posted by jrhitokiri
                          glad i could be of help.. ^^,

                          and for your next problem, what happens in IE that does not happen in mozilla..? can you expound on the problem..?
                          The solution you have suggested (to add session_start() in index.php) is working in IE but not in mozilla. In mozilla firefox the problem still persists.

                          Comment

                          • jrhitokiri
                            New Member
                            • Apr 2008
                            • 35

                            #14
                            try to take out these lines from your login.php:

                            Code:
                            session_register('USERNAME');
                            session_register('SESS_LOGGEDIN');

                            Comment

                            • urfriend202
                              New Member
                              • Nov 2008
                              • 9

                              #15
                              I tried but of no use.......

                              Comment

                              Working...