Question

I am new in php, while running i am getting an error in almost every page that "Undefined index........." dont know what to do with it

Below is one of the example of login page -

Thanks for the tips.....

<?php
ob_start();//Starting output buffering to avoid the Headers Already Sent error on some installations

//Todo: "e-mail forgotten password" function
//Todo: make the logo uploadable in the admin panel
//Localisation: none

//Adding the page header
include 'includes/page_header_simple.php';

//Load the language file
include 'includes/language.php';

//Assigning values already entered (if any) to variables
$login_username = strip_tags($_POST['login_username']);
$login_password = strip_tags($_POST['login_password']);
$button_pressed = $_POST['action_button'];

if ($button_pressed == "Login") {

    //***************************************************************************************************************************************
    //Validate the form if the "Submit" button was pressed in the Registration Form
    //***************************************************************************************************************************************

    //Check if the user has entered values in both input fields
    if((!$login_username) || (!$login_password)) {

        //Javascript redirect function is used as the PHP header() function creates errors in some installations
        ?>

        <script type="text/javascript">
        <!--
        window.location = "frm_login.php"
        -->
        </script>

        <?php

        exit();

    }

    //Find the username in the database
    //Connect to the database
    include 'includes/config.php';
    include 'includes/opendb.php';

    $sql = "SELECT user_id, tp_username, user_password, first_name, last_name, usergroup FROM users WHERE tp_username = '$login_username' AND enabled = '1'";
    $result = mysql_query($sql) or die('Sorry, could not complete the login process. If the problem persists, please send the following error report to the administrator: ' . mysql_error());

    if (mysql_num_rows($result) == 1) {

        $row = mysql_fetch_array($result);
        $user_password = $row['user_password'];

        //Carry out password check
        if(crypt($login_password, $user_password) == $user_password) {

            //Set the Session and move to the the Prospects page if the login is successful
            //Start the session
            session_start();

            $_SESSION["logged_in"] = true;
            $_SESSION['first_name'] = mysql_result($result, 0, "first_name");
            $_SESSION['last_name'] = mysql_result($result, 0, "last_name");
            $_SESSION['usergroup'] = mysql_result($result, 0, "usergroup");
            $_SESSION['user_id'] = mysql_result($result, 0 , "user_id");
            $_SESSION['sort_by'] = "pipeline_items.probability";
            $_SESSION['sort_order'] = "DESC";
            $_SESSION['geography_filter'] ="%";
            $_SESSION['product_service_filter'] ="%";
            $_SESSION['value_filter'] = "1";
            $_SESSION['sales_stage_filter'] = "%" ;
            $_SESSION['probability_filter'] = "%";

            //Javascript redirect function is used as the PHP header() function creates errors in some installations
        ?>

            <script type="text/javascript">
            <!--
            window.location = "tbl_pipeline_items.php"
            -->
            </script>

            <?php

            exit();

        } else {

            //If the user authentication failed, display an error message and reload the Login Form
            include 'includes/closedb.php';

            echo("<p align='center' class='gen_text'>The username/password combination you have used is invalid or your account has not yet been activated by the administrator.</p>");

            //This form includes a single button that allows users to return to the registration form after an unsuccessful registration  -->
            echo ("<form name='back_to_login' method='post' action='frm_login.php'>");
            echo ("<p align='center'><input type='submit' name='submit' value='Return to Login'></p>");

            //Insert the footer
            echo ("<table border='0' width='100%' align='center'>");
            echo ("<tr>");
            echo ("<td colspan='2'><hr size='1'></td>");
            echo ("</tr>");
            echo ("<tr>");
            echo ("<td><p align='left' class='sm_gen_footer'>$VERSION</p></td>");
            echo ("<td><div align='right'><a href='http://theplumber.whsites.net' target='_blank'><img src='images/poweredby.gif' alt='Powered by thePlumber' width='80' height='15' border='0'></a></div></td>");
            echo ("</tr>");
            echo ("</table>");

        }

    } else {

        //Display an error message if the username entered in the Login Form does not exist in the database
        include 'includes/closedb.php';

        echo("<p align='center' class='gen_text'>The username/password combination you have used is invalid or your account has not yet been activated by the administrator.</p>");

        //This form includes a single button that allows users to return to the registration form after an unsuccessful registration  -->
        echo ("<form name='back_to_login' method='post' action='frm_login.php'>");
        echo ("<p align='center'><input type='submit' name='submit' value='Return to Login'></p>");

        //Insert the footer
        echo ("<table border='0' width='100%' align='center'>");
        echo ("<tr>");
        echo ("<td colspan='2'><hr size='1'></td>");
        echo ("</tr>");
        echo ("<tr>");
        echo ("<td><p align='left' class='sm_gen_footer'>$VERSION</p></td>");
        echo ("<td><div align='right'><a href='http://theplumber.whsites.net' target='_blank'><img src='images/poweredby.gif' alt='Powered by thePlumber' width='80' height='15' border='0'></a></div></td>");
        echo ("</tr>");
        echo ("</table>");

    }

}

if ($button_pressed == "") {

    //***************************************************************************************************************************************
    //Display the Login Form if the "$button_pressed" variable is empty 
    //***************************************************************************************************************************************

    ?>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title> thePlumber </title>
    <meta name="Generator" content="Dev-PHP 2.2.2">
    <link rel="stylesheet" href="styles/default.css">
    <style type="text/css">
    </style>
    </head>

    <body>
    <!-- This file displays the Login form -->
    <table border="0" width="80%"  border="0" align="center" cellpadding="0" cellspacing="3">
        <tr>
            <td>
            <form name="user_login" method="post" action="frm_login.php">
                <table border="0" cellpadding="0" cellspacing="0" bordercolor="#FFFFFF">
                    <tr height="20" bgcolor="b51109">
                        <td valign="top"><div align="left"><img src="images/red_tl.png" width="17" height="17"></div></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td valign="top"><div align="right"><img src="images/red_tr.png" width="17" height="17"></div></td>
                    </tr>
                    <tr height="40" bgcolor="b51109">
                        <td></td>
                        <td rowspan="2"><img src='images/login.png' width='64' height='64' border='0'></td>
                        <td valign="middle"><div align="left" class="login_text"><label for="login_username">Username:</label></div></td>
                    <td valign="middle"><div align="left"><input type="text" size="20" length="25" name="login_username" value=""></div></td>
                    <td></td>
                </tr>   
                    <tr height="40" bordercolor="b51109" bgcolor="#b51109">
                        <td></td>
                        <td valign="middle"><div align="left" class="login_text"><label for="login_password">Password:</label></div></td>
                        <td valign="middle"><div align="left"><input type="password" size="20" length="25" name="login_password" value=""></div></td>
                        <td></td>
                    </tr>
                    <tr height="40" bordercolor="b51109" bgcolor="b51109">
                        <td valign="bottom"><div align="left"><img src="images/red_bl.png" width="17" height="17"></div></td>
                        <td></td>
                        <td></td>
                        <td valign="middle"><div align="right"><input type="submit" name="action_button" value="Login"></div></td>
                        <td valign="bottom"><div align="right"><img src="images/red_br.png" width="17" height="17"></div></td>
                    </tr>
                </table>
            </form>
            <p align="left"><a class="login_small_links" href="frm_registration.php">Need a new user account? Register here.</a>
            </td>
            <td width="10"></td>
            <td width="100%" valign="top"><p align="left" class="gen_text">Welcome to the sales pipeline management system. Please login to continue.</p></td>
        </tr>
    </table>

    <?php

    //Inserting the footer
    echo ("<table border='0' width='100%' align='center'>");
    echo ("<tr>");
    echo ("<td colspan='2'><hr size='1'></td>");
    echo ("</tr>");
    echo ("<tr>");
    echo ("<td><p align='left' class='sm_gen_footer'>$VERSION</p></td>");
    echo ("<td><div align='right'><a href='http://theplumber.whsites.net' target='_blank'><img src='images/poweredby.gif' alt='Powered by thePlumber' width='80' height='15' border='0'></a></div></td>");
    echo ("</tr>");
    echo ("</table>");

}

ob_flush();//End output buffering
?>

</body>
</html>
Was it helpful?

Solution

I noticed you are not using any isset on your $_REQUEST / $_POST variables... I suggest you read this guide and use isset() to debug which one might be causing the problem!
http://siliconstation.com/how-fix-php-notice-undefined-index/

OTHER TIPS

NOTICE errors are not important in PHP but they can help to debug the old codes.

This error means you used a variable in your code that you didn't define it before, you have 2 ways to solve it :

  1. turn off notice in error reporting ( I recommend this )

    put error_reporting(E_ALL ^ E_NOTICE); in the first line of your application

  2. define variable before using it, or put a condition before using it like :

    if(isset($variable)) echo $variable;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top