php undefined index in

时间:2016-07-15 09:40:59

标签: php apache xampp

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../style.css"/>
        <title>
        Metin2 Bash-Panel by .Infinity - ChatBlock-Panel
        </title>
    </head>
    <body>
    <center>
    <img src="../images/head.jpg"/>
    </center>
    <div id="main">
    <center>
    <i>
    <b>
    <a href="../index.php"> Panel-Auswahl </a>
    <a href="index.php"> Startseite </a>
    <a href="shutdown_p2p.php"> Server herunterfahren </a>
    <a href="notice_p2p.php"> Ankündigungen schreiben </a>
    <a href="block_chat_p2p.php"> ChatBlock-Panel </a>
    <a href="dc_p2p.php"> DC-Panel </a>
    <a href="rc_p2p.php"> RufChat-Panel </a>
    </b>
    </i>
    <div id="php">

<?php
/* block_chat_p2p.php by .Infinity

*/

// Variablensetzung

$IP = $_POST['ip'];
$LogData_IP = $_SERVER['REMOTE_ADDR'];
$Port = $_POST['port'];
$Submit = $_POST['submit'];
$Player = $_POST['player'];
$Time = $_POST['time'];

if($Submit)
{
    if(strlen($IP) < 1)
    {
        echo "<br /><br /><b>Bitte gebe eine IP an!</b><br />";
    }

    if(strlen($Port) < 1)
    {
        echo "<br /><br /><b>Bitte gebe einen Port an!</b><br />";
    }

    if(empty($Player))
    {
        echo "<br /><br /><b>Bitte geben sie einen Spielernamen ein!</b><br />";
    }

}

if($IP && $Port && $Player)
{
    $LogData = fopen("../log/log_data_block_chat_p2p.txt", "a");
    fwrite($LogData, "$IP:$Port from $LogData_IP");
    fwrite($LogData, "\n");
    fclose($LogData);
    $socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
    $result = socket_connect ($socket, $IP, $Port);

    $msg = $Player;
    $time = "100";
    $msgsize = strlen($msg);
    $maximumsize = "25";

    $query = "\x16".$msg;

    $leftsize = $maximumsize - $msgsize;

    while($leftsize)
    {
            $query = $query."\x00";
            $leftsize = $leftsize - 1;
    }

    $query = $query.$time."\x00";

    $query_size = strlen($query);
    $write_res = socket_write($socket, $query, $query_size);
    $recv_res = socket_recv($socket, $output, 5056, 0);
    echo "<br />";
    echo "<br />";
    echo "<b>Der Spieler ".$Player." hat einen Chatblock erhalten.</b>";
    socket_close($socket);
}
?>

    </div>
    <form method="POST">
    <div id="input">
    <b>
    <br />
    <br />
    Bitte geben sie eine ServerIP ein!
    <br />
    <br />
    <input type="text" name="ip"/>
    <br />
    <br />
    Bitte geben sie einen P2PPort ein!
    <br />
    <br />
    <input type="text" name="port"/>
    <br />
    <br />
    Bitte geben sie einen SpielerNamen ein!
    <br />
    <br />
    <input type="text" name="player"/>
    <br />
    <br />
    <br />
    </div>
    <input class="submit" type="submit" name="submit" value="ChatBlock setzen!"/>
    <br />
    <br />
    </b>
    </form>
    </center>
    </div>
    </body>
</html>

有人能告诉我这个小代码有什么问题吗?我刚开始学习php和html ..我收到这个错误 注意:未定义的索引:第34行的C:\ xampp \ htdocs \ p2p \ block_chat_p2p.php中的ip

注意:未定义的索引:第36行的C:\ xampp \ htdocs \ p2p \ block_chat_p2p.php中的端口

注意:未定义的索引:在第37行的C:\ xampp \ htdocs \ p2p \ block_chat_p2p.php中提交

注意:未定义的索引:第38行的C:\ xampp \ htdocs \ p2p \ block_chat_p2p.php中的播放器

注意:未定义的索引:第39行的C:\ xampp \ htdocs \ p2p \ block_chat_p2p.php中的时间

6 个答案:

答案 0 :(得分:1)

您好请用下面的代码替换。请使用$_SERVER['REQUEST_METHOD']

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../style.css"/>
        <title>
        Metin2 Bash-Panel by .Infinity - ChatBlock-Panel
        </title>
    </head>
    <body>
    <center>
    <img src="../images/head.jpg"/>
    </center>
    <div id="main">
    <center>
    <i>
    <b>
    <a href="../index.php"> Panel-Auswahl </a>
    <a href="index.php"> Startseite </a>
    <a href="shutdown_p2p.php"> Server herunterfahren </a>
    <a href="notice_p2p.php"> Ankündigungen schreiben </a>
    <a href="block_chat_p2p.php"> ChatBlock-Panel </a>
    <a href="dc_p2p.php"> DC-Panel </a>
    <a href="rc_p2p.php"> RufChat-Panel </a>
    </b>
    </i>
    <div id="php">

<?php
/* block_chat_p2p.php by .Infinity

*/

// Variablensetzung
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$IP = $_POST['ip'];
$LogData_IP = $_SERVER['REMOTE_ADDR'];
$Port = $_POST['port'];
$Submit = $_POST['submit'];
$Player = $_POST['player'];
$Time = $_POST['time'];

    if($Submit)
    {
        if(strlen($IP) < 1)
        {
            echo "<br /><br /><b>Bitte gebe eine IP an!</b><br />";
        }

        if(strlen($Port) < 1)
        {
            echo "<br /><br /><b>Bitte gebe einen Port an!</b><br />";
        }

        if(empty($Player))
        {
            echo "<br /><br /><b>Bitte geben sie einen Spielernamen ein!</b><br />";
        }

    }

    if($IP && $Port && $Player)
    {
        $LogData = fopen("../log/log_data_block_chat_p2p.txt", "a");
        fwrite($LogData, "$IP:$Port from $LogData_IP");
        fwrite($LogData, "\n");
        fclose($LogData);
        $socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP);
        $result = socket_connect ($socket, $IP, $Port);

        $msg = $Player;
        $time = "100";
        $msgsize = strlen($msg);
        $maximumsize = "25";

        $query = "\x16".$msg;

        $leftsize = $maximumsize - $msgsize;

        while($leftsize)
        {
                $query = $query."\x00";
                $leftsize = $leftsize - 1;
        }

        $query = $query.$time."\x00";

        $query_size = strlen($query);
        $write_res = socket_write($socket, $query, $query_size);
        $recv_res = socket_recv($socket, $output, 5056, 0);
        echo "<br />";
        echo "<br />";
        echo "<b>Der Spieler ".$Player." hat einen Chatblock erhalten.</b>";
        socket_close($socket);
    }
}
?>

    </div>
    <form method="POST">
    <div id="input">
    <b>
    <br />
    <br />
    Bitte geben sie eine ServerIP ein!
    <br />
    <br />
    <input type="text" name="ip"/>
    <br />
    <br />
    Bitte geben sie einen P2PPort ein!
    <br />
    <br />
    <input type="text" name="port"/>
    <br />
    <br />
    Bitte geben sie einen SpielerNamen ein!
    <br />
    <br />
    <input type="text" name="player"/>
    <br />
    <br />
    <br />
    </div>
    <input class="submit" type="submit" name="submit" value="ChatBlock setzen!"/>
    <br />
    <br />
    </b>
    </form>
    </center>
    </div>
    </body>
</html>

答案 1 :(得分:0)

变量是不变的,因为您声明它们但没有按下提交按钮。所以你必须这样做。

if(isset($_POST['submit'])){
    $IP = $_POST['ip'];
    $LogData_IP = $_SERVER['REMOTE_ADDR'];
    $Port = $_POST['port'];
    $Player = $_POST['player'];
    $Time = $_POST['time'];

    //rest of the code
}

答案 2 :(得分:0)

您需要使用isset

使用此if(isset($_POST['submit'])){代替if($Submit){

加载页面时显示未定义的索引

if(isset($_POST['submit'])){

//write ur code inside this

    $IP = $_POST['ip'];
    $LogData_IP = $_SERVER['REMOTE_ADDR'];
    $Port = $_POST['port'];
    $Submit = $_POST['submit'];
    $Player = $_POST['player'];
    $Time = $_POST['time'];

.....so on

}

答案 3 :(得分:0)

试一试:

<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../style.css"/>
        <title>
            Metin2 Bash-Panel by .Infinity - ChatBlock-Panel
        </title>
    </head>
    <body>
    <center>
        <img src="../images/head.jpg"/>
    </center>
    <div id="main">
        <center>
            <i>
                <b>
                    <a href="../index.php"> Panel-Auswahl </a>
                    <a href="index.php"> Startseite </a>
                    <a href="shutdown_p2p.php"> Server herunterfahren </a>
                    <a href="notice_p2p.php"> Ankündigungen schreiben </a>
                    <a href="block_chat_p2p.php"> ChatBlock-Panel </a>
                    <a href="dc_p2p.php"> DC-Panel </a>
                    <a href="rc_p2p.php"> RufChat-Panel </a>
                </b>
            </i>
            <div id="php">

                <?php
                /* block_chat_p2p.php by .Infinity

                 */

// Variablensetzung
                if (isset($_POST['submit'])) {
                    $IP = isset($_POST['ip']) ? $_POST['ip'] : "";
                    $LogData_IP = $_SERVER['REMOTE_ADDR'];
                    $Port = isset($_POST['port']) ? $_POST['port'] : "";

                    $Player = isset($_POST['player']) ? $_POST['player'] : "";
                    $Time = isset($_POST['time']) ? $_POST['time'] : "";


                    if (strlen($IP) < 1) {
                        echo "<br /><br /><b>Bitte gebe eine IP an!</b><br />";
                    }

                    if (strlen($Port) < 1) {
                        echo "<br /><br /><b>Bitte gebe einen Port an!</b><br />";
                    }

                    if (empty($Player)) {
                        echo "<br /><br /><b>Bitte geben sie einen Spielernamen ein!</b><br />";
                    }

                    if ($IP && $Port && $Player) {
                        $LogData = fopen("../log/log_data_block_chat_p2p.txt", "a");
                        fwrite($LogData, "$IP:$Port from $LogData_IP");
                        fwrite($LogData, "\n");
                        fclose($LogData);
                        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                        $result = socket_connect($socket, $IP, $Port);

                        $msg = $Player;
                        $time = "100";
                        $msgsize = strlen($msg);
                        $maximumsize = "25";

                        $query = "\x16" . $msg;

                        $leftsize = $maximumsize - $msgsize;

                        while ($leftsize) {
                            $query = $query . "\x00";
                            $leftsize = $leftsize - 1;
                        }

                        $query = $query . $time . "\x00";

                        $query_size = strlen($query);
                        $write_res = socket_write($socket, $query, $query_size);
                        $recv_res = socket_recv($socket, $output, 5056, 0);
                        echo "<br />";
                        echo "<br />";
                        echo "<b>Der Spieler " . $Player . " hat einen Chatblock erhalten.</b>";
                        socket_close($socket);
                    }
                }
                ?>

            </div>
            <form method="POST">
                <div id="input">
                    <b>
                        <br />
                        <br />
                        Bitte geben sie eine ServerIP ein!
                        <br />
                        <br />
                        <input type="text" name="ip"/>
                        <br />
                        <br />
                        Bitte geben sie einen P2PPort ein!
                        <br />
                        <br />
                        <input type="text" name="port"/>
                        <br />
                        <br />
                        Bitte geben sie einen SpielerNamen ein!
                        <br />
                        <br />
                        <input type="text" name="player"/>
                        <br />
                        <br />
                        <br />
                </div>
                <input class="submit" type="submit" name="submit" value="ChatBlock setzen!"/>
                <br />
                <br />
                </b>
            </form>
        </center>
    </div>
</body>
</html>

答案 4 :(得分:0)

似乎页面没有收到POST请求中的值。确保它已设置。

<?php
    var_dump($_POST); // For debug purposes not a production code
?>

如果你想确定变量将被设置使用

<?php
if(!empty($_POST['submit'])) {
    $IP = !empty($_POST['ip']) ? $_POST['ip'] : '';
    $LogData_IP = $_SERVER['REMOTE_ADDR'];
    $Port = !empty($_POST['port']) ? $_POST['port'] : '';
    $Submit = !empty($_POST['submit']) ? $_POST['submit'] : '';
    $Player = !empty($_POST['player']) ? $_POST['player'] : '';
    $Time = !empty($_POST['time']) ? $_POST['time'] : '';
}
?>

稍后使用一些错误消息正确处理空状态。

答案 5 :(得分:0)

创建一个单独的php文件,并在表单中的action属性中提及该文件名。

file.php

<?php
                /* block_chat_p2p.php by .Infinity

                 */

// Variablensetzung
                $Submit = $_POST['submit'];
                if ($Submit) {
                    $IP = ($_POST['ip']) ? $_POST['ip'] : "";
                    $LogData_IP = $_SERVER['REMOTE_ADDR'];
                    $Port = ($_POST['port']) ? $_POST['port'] : "";

                    $Player = ($_POST['player']) ? $_POST['player'] : "";
                    $Time = ($_POST['time']) ? $_POST['time'] : "";


                    if (strlen($IP) < 1) {
                        echo "<br /><br /><b>Bitte gebe eine IP an!</b><br />";
                    }

                    if (strlen($Port) < 1) {
                        echo "<br /><br /><b>Bitte gebe einen Port an!</b><br />";
                    }

                    if (empty($Player)) {
                        echo "<br /><br /><b>Bitte geben sie einen Spielernamen ein!</b><br />";
                    }

                    if ($IP && $Port && $Player) {
                        $LogData = fopen("../log/log_data_block_chat_p2p.txt", "a");
                        fwrite($LogData, "$IP:$Port from $LogData_IP");
                        fwrite($LogData, "\n");
                        fclose($LogData);
                        $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
                        $result = socket_connect($socket, $IP, $Port);

                        $msg = $Player;
                        $time = "100";
                        $msgsize = strlen($msg);
                        $maximumsize = "25";

                        $query = "\x16" . $msg;

                        $leftsize = $maximumsize - $msgsize;

                        while ($leftsize) {
                            $query = $query . "\x00";
                            $leftsize = $leftsize - 1;
                        }

                        $query = $query . $time . "\x00";

                        $query_size = strlen($query);
                        $write_res = socket_write($socket, $query, $query_size);
                        $recv_res = socket_recv($socket, $output, 5056, 0);
                        echo "<br />";
                        echo "<br />";
                        echo "<b>Der Spieler " . $Player . " hat einen Chatblock erhalten.</b>";
                        socket_close($socket);
                    }
                }
                ?>

现在将action属性添加到表单并提及此文件。 像

<form method="POST" action='file.php'>

希望它会有所帮助。