Session Error in PHP! ( Newbee question )

Discussion in 'PHP' started by eritrea1, May 19, 2012.

  1. #1
    Hello Eveybody.
    I have a simple PHP Log-in / Log-out script. I can log in just fine, but i keep on getting these exasperating errors.

    This is what i see on my page, when i log in using this script.

    Notice: Use of undefined constant myusername - assumed 'myusername' in C:\xampp\htdocs\DIRECTORY\LOGIN\login_success.php on line 3

    Deprecated: Function session_is_registered() is deprecated in C:\xampp\htdocs\DIRECTORY\LOGIN\login_success.php on line 3
    Login Successful



    And this is the actual code in login_success.php From which the problem is emanating from:

    <?
    session_start();
    if(!session_is_registered(myusername)){
    header("location:main_login.php");
    }
    ?>


    <html>
    <body>
    Login Successful
    </body>
    </html>




    Thanks for you help in Advance.
     
    Last edited: May 19, 2012
    eritrea1, May 19, 2012 IP
  2. tacnix

    tacnix Member

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    43
    #2
    Try this...

    Case 1: Single page case.
    
    // Single page(Login-page: login.php).
    session_start();
    
    // First check that you are registering session with "myusername", before if-else condition. 
    session_register(myusername);
    
    // Now try, the checking session is registered or not.
    if(!session_is_registered('myusername')) {
       header("location:main_login.php");
    }
    
    PHP:
    Case 2: Multiple page case.
    
    // Page one(Login-page: login.php)
    session_start();
    
    // Register session into first page(login-page).
    session_register('myusername');
    
    // Second page(Login-success-page: login_success.php).
    // Get the value from registery/saved session on the browser.
    $get_session = $_SESSION['myusername'];
    
    // Now check the session with "$get_session" variable for its registered or not.
    if(!session_is_registered($get_session)) {
       header('location:main_login.php');
    }
    
    PHP:

    Note:
    Without registering any session(session_register()) you can't check that your any session is registered(session_is_registered()) or not.
     
    tacnix, May 19, 2012 IP
  3. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #3
    For your first error, that is simply a notice. There are 2 options,
    a) You can attempt to resolve it by putting session_register('myusername'); just after the session_start();
    b) Alternatively you can just change the level of error reporting on your PHP installation. Simply open up PHP.ini in "C:\xampp\php\" and set the error reporting to something else a full list of error reporting levels can be found http://uk3.php.net/manual/en/errorfunc.constants.php.

    As for your second error, as session_is_registered has been removed from PHP on versions 5.4.0 and above you need to change your code to something like this

    <? 
    session_start();
    if(isset($_SESSION['myusername'])){
    header("location:main_login.php");
    }
    ?>
    PHP:
     
    PK-Host, May 20, 2012 IP
  4. eritrea1

    eritrea1 Active Member

    Messages:
    182
    Likes Received:
    9
    Best Answers:
    2
    Trophy Points:
    70
    #4
    Thanks for your response, but it still is not working.
    btw in checkloging.php i have this code ( in regards to the session).. if it helps.
    -----------------------------------------------------
    <?php


    if($count==1){
    // Register $myusername, $mypassword and redirect to file "login_success.php"
    session_register("myusername");
    session_register("mypassword"); 
    header("location:login_success.php");
    }
    else {
    echo "Wrong Username or Password";
    }
    ?>
    ----------------------------------------------------


    I tried to replace both your examples in the source code, but i am unable to log in after that. Also, there is a conflict between the two of your comments.
    Thanks for your help so far.
     
    eritrea1, May 20, 2012 IP
  5. PK-Host

    PK-Host Guest

    Messages:
    109
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    0
    #5
    eritrea1 I wouldnt recommend storing your username and password within a session. I would recommend checking them on the login form against what is logged in the database then storing the $_SESSION['PHPSESSID'] which is unique each time a visitor goes to your website in a database against a user. Then if that PHPSESSID is valid then log them in.
     
    PK-Host, May 20, 2012 IP