Web Analytics Made Easy -
StatCounter PHP coding error - CodingForum

Announcement

Collapse
No announcement yet.

PHP coding error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new 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
    Replace this
    PHP Code:
    if($_POST['logout']==true
    with this
    PHP Code:
    if (isset($_POST['logout']) && $_POST['logout'] == "true"

    Comment


    • #3
      You should also start the sessions as your first line in this script.
      Evolution - The non-random survival of random variants.
      Physics is actually atoms trying to understand themselves.

      Comment


      • #4
        session_register is deprecated. Instead use $_SESSION['variable-name'].
        "Tango says double quotes with a single ( ' ) quote in the middle"
        '$Name says single quotes with a double ( " ) quote in the middle'
        "Tango says double quotes ( \" ) must escape a double quote"
        '$Name single quotes ( \' ) must escape a single quote'

        Comment

        Working...
        X