Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP coding error

    Hey guys I keep getting an error on line 49 saying undefined index can anyone help me wit this?


    PHP Code:

    <?php 

        $uri 
    $_SERVER['REQUEST_URI'];
        function 
    displaylogon($alert$uri)
        {

                echo 
    "<form method='post' action='login.php'>";

                echo 
    "Login Name" ;
                echo 
    "<input type='text' name='loginname' size='15'/>";
                echo 
    "Login Password";
                echo 
    "<input type='password' name='loginpassword'  maxlength='15' size='15'/>";
                echo 
    "<span class='alert'>$alert</span>";
                echo 
    "<input type='submit' name='loginbutton' value='Login' />";
                echo 
    "</form>";

        }
        function 
    displaywelcome($uri)
        {
                echo 
    "<p>";
                
                if(
    $_SESSION['admin']==true)
                {
                    echo 
    "Welcome Administrator";
                    echo 
    "<br/>";
                    echo 
    "<br/>";
                    echo 
    "<a href='../admin/admin.php'>Admin Page</a>";
                }
                else
                {
                    echo 
    "Welcome " $_SESSION['firstname'];            
                    echo 
    "<br/>";
                    echo 
    "<br/>";
                    echo 
    "<a href='../main/cart.php'>View Cart</a>";                
                }
                echo 
    "</p>";
                
                echo 
    "<form action='../main/homepage.php' method='post'>";
                    echo 
    "<table>";
                        echo 
    "<tr>";
                            echo 
    "<td><input type='hidden' value='true' name='logout' />";
                            echo 
    "<input type='submit' value='Log out' /></td>";
                        echo 
    "</tr>";
                    echo 
    "</table>";
                echo 
    "</form>";
        }
        
        if(
    $_POST['logout']==true)
        {    
            
    //log the user out
            
    session_destroy();

            
    $alert =  "You are now logged out.";    
            
    displaylogon($alert$uri);
        }
        else
        {

            if (isset(
    $_SESSION['username']))
            {
                if(
    $_POST['logout']==true)
                {    
                    
    //log the user out
                    
    session_destroy();
                    
    header('location:..\homepage.php?');
                    
    //$alert =  "You are now logged out.";    
                    //displaylogon($alert, $uri);
                
    }
                else
                {
                    
    displaywelcome($uri);
                }
            }
            else if(isset(
    $_POST['loginname']))
            {
                
    //check for admin login
                
    if((strcmp("super",$_POST['loginname'])==0)&& (strcmp("super",$_POST['loginpassword'])==0))
                {
                    
    //set up session variables
                    
    session_register('firstname');
                    
    session_register('username');
                    
    session_register('admin');
                    
    $_SESSION['admin']=true;
                    
    $_SESSION['firstname']= 'super';
                    
    $_SESSION['username']= 'super';
                    
                    
    displaywelcome($uri);
                            
                }
                else
                {
                    
    //check user name is valid
                    
    $fp fopen("data.txt","r");
                    if (
    $fp == null)
                    {
                        
    $alert =  "An error has occurred, please try again.";
                        
    displaylogon($alert$uri);
                    }
                    else
                    {
                        
    $match false;
                        while (!
    feof($fp))
                        {
                            
    $line fgetss($fp);
                            
    //only check if line from file is not blank.
                            
    if (!(($line =="") ||  ($line==null)))
                            {
                                
    $linearray explode("\t",$line);
                                
    //check if username and password match
                                
    if((strnatcasecmp($linearray[2],$_POST['loginname'])==0)&& (strcmp($linearray[0],$_POST['loginpassword'])==0))
                                {
                                    
    $match true;
                                }
                            }
                        }
                        
                        if (
    $match == true)
                        {
                            
    //set up session variables
                            
    session_register('firstname');
                            
    session_register('username');
                            
    $_SESSION['firstname']= $_POST['loginpassword'];
                            
    $_SESSION['username']= $_POST['loginname'];
                            
                            
    displaywelcome($uri);
                            
                            
                        }
                        else
                        {
                            
    $alert =  "Username or password is incorrect.";
                            
    displaylogon($alert$uri);
                        }
                        
    fclose($fp);
                    }
                }        
            }
            else
            {
                
    //send the user back to where they came from
                
    $alert="";
                
    displaylogon($alert$uri);
                
        
            }
        }
    ?>

  • #2
    Regular Coder
    Join Date
    May 2011
    Posts
    240
    Thanks
    1
    Thanked 56 Times in 55 Posts
    Replace this
    PHP Code:
    if($_POST['logout']==true
    with this
    PHP Code:
    if (isset($_POST['logout']) && $_POST['logout'] == "true"

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,354
    Thanks
    23
    Thanked 618 Times in 617 Posts
    You should also start the sessions as your first line in this script.

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,346
    Thanks
    60
    Thanked 527 Times in 514 Posts
    session_register is deprecated. Instead use $_SESSION['variable-name'].
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •