$sql="SELECT Member_ID, PASSWORD FROM member WHERE member_ID='$member_ID' and password='$password'";
 $result=mysql_query($sql);
 $count=mysql_num_rows($result);
if($count==1){
 session_register("member_ID");
 session_register("password"); 
 header("location:login_success.php");
 }
 else {
 echo "Wrong Username or Password";
 }

I have this code for a login code, problem is session register, I get this error Fatal error: Call to undefined function session_register(), and when I searched I read it cant be used anymore, so what can I use instead? thanks

    $sql="SELECT Member_ID, PASSWORD FROM member 
    
    WHERE member_ID='$member_ID' and 
    
    password='$password'";
     $result=mysql_query($sql);
     $count=mysql_num_rows($result);
    if($count==1){
     $_session['member_ID']=$member_ID;
     $_session['password']=$password; 
     header("location:login_success.php");
     }
     else {
     echo "Wrong Username or Password";
     } 

    think this would fix it right?

      meme;11044357 wrote:
      $sql="SELECT Member_ID, PASSWORD FROM member 
      
      WHERE member_ID='$member_ID' and 
      
      password='$password'";
       $result=mysql_query($sql);
       $count=mysql_num_rows($result);
      if($count==1){
       $_session['member_ID']=$member_ID;
       $_session['password']=$password; 
       header("location:login_success.php");
       }
       else {
       echo "Wrong Username or Password";
       } 

      think this would fix it right?

      and this is the code in login success page,

      <?php
          session_start();
          if(isset($_SESSION[$member_ID])){
              echo "welcome to protected page.";
          }
          else {  
      header('location:login.php'); exit;
      } ?> <html> <body> Login Successful </body>

      I think I fixed the first one but couldn't fix the second, what do you guys think?

        First off, run this and inspect the output.

        $_SESSION['id'] = 1;
        $_session['id'] = 2;
        echo $_SESSION['id'] . '<br>' . $_session['id'];
        

        Then, can you spot a difference between where you assign and access data below, not related to the issue in the example above

        # assign 
        if($count==1){ 
         $_session['member_ID']=$member_ID; 
        
        # access
        $_SESSION[$member_ID]
        

          If I'm not mistaken, you have to call [man]session_start[/man] before you either check or assign values to any $SESSION values. Once you do that, you can set any values you like in $SESSION and they should be available on subsequent page requests.

          Your use of mysql functions is bad for a few reasons:
          1) I don't see any code where you use mysql_connect
          2) You don't bother to check if the connections or queries result in any errors.
          3) The mysql
          functions are really old now and have been deprecated. Please see details in my signature.

            johanafm;11044365 wrote:

            First off, run this and inspect the output.

            $_SESSION['id'] = 1;
            $_session['id'] = 2;
            echo $_SESSION['id'] . '<br>' . $_session['id'];
            

            Then, can you spot a difference between where you assign and access data below, not related to the issue in the example above

            # assign 
            if($count==1){ 
             $_session['member_ID']=$member_ID; 
            
            # access
            $_SESSION[$member_ID]
            

            so I have to add echo $_session ['member_ID']...etc?

            no I don't know what is the difference between assign and acess, is my mistake harder than what I think? I don't know what I am doing to be honest :/ sadly

              sneakyimp;11044377 wrote:

              If I'm not mistaken, you have to call [man]session_start[/man] before you either check or assign values to any $SESSION values. Once you do that, you can set any values you like in $SESSION and they should be available on subsequent page requests.

              Your use of mysql functions is bad for a few reasons:
              1) I don't see any code where you use mysql_connect
              2) You don't bother to check if the connections or queries result in any errors.
              3) The mysql
              functions are really old now and have been deprecated. Please see details in my signature.

              I added start session at the top of page but didn't work, I did the sql connect but I didn't copy it cause its abit too long, I mean in the first page, second page I dint think I need to, do I? I do care but I don't know how, I am not a professional I am a student and I am still learning, not as fast as I would like but I will get there eventually, and we are using MySQL because the instructor asked us to, not my choice I don't know any other way...

                MySQL can mean many things. Sneaky is talking about the client interface, and using mysqli functions instead of mysql functions. It's quite simple, really ... mostly an extra "i" and an extra parameter to the calls to indicate the server connection being used:

                //old
                mysql_connect($host,$user,$pass);
                mysql_select_db($dbname);
                mysql_query($sql);
                
                //new
                $connection = mysqli_connect($host,$user,$pass,$dbname);
                mysqli_query($connection,$sql);
                

                  One of the points was being made above is that the super-global array $_SESSION is spelled with all capital letters. 🙂

                  As far as session_register() goes...

                  Warning This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 5.4.0.

                  ...so just say "no" to it. Any tutorial or 3rd-party code making use of it should probably be avoided as being well out-of-date with current PHP bets practices.

                    meme;11044385 wrote:

                    I added start session at the top of page but didn't work, I did the sql connect but I didn't copy it cause its abit too long, I mean in the first page, second page I dint think I need to, do I? I do care but I don't know how, I am not a professional I am a student and I am still learning, not as fast as I would like but I will get there eventually, and we are using MySQL because the instructor asked us to, not my choice I don't know any other way...

                    Like I said, before you ever refer to $_SESSION or the values it contains, you first have to call [man]session_start[/man]. This is mandatory or you are just going to end up confusing yourself and having code that does not work. I'd recommend reading the manual page I just linked there if you haven't already. It might not all make sense, but hopefully some of it will start to stick and will make sense later. The basic idea is that you can't start working with PHP's session handling until you tell it to start.

                    I can certainly appreciate that some code is too long to post. The alternative is that people suggest that you've made a mistake when you actually haven't.

                    And YES every single page request on your server must reconnect to the MySQL server before you can run any queries. This is why I pointed out that you aren't checking for any errors from your mysql statements. If you were, you would see that an error is generated if you try to run a query without first successfully connecting to the database. If you check for errors when you connect to the database, you might see errors generated if that connection fails.

                    As for your instructor telling you to use mysql, you might or might not want to tell him/her that [man]mysql[/man] has been deprecated (i.e., retired) in favor of [man]mysqli[/man]. I'd also recommend reading the manual a bit. It's full of examples that actually work. It might take a little extra time to read it, but you'll probably be saving all that much-more-frustrating time where you just sit around wondering why it's not working.

                      Write a Reply...