0

I am currently working a little with PHP and taking over a project that wasn't created by me. The tricky things is that on the server/webhotel the application is working like expected, however in my local test environment (WAMP 3.0.6 64bit, PHP 5.6.25, Apache 2.4.23, MySQL 5.7.14) it isn't working. The error message that is being output by PHP is :

Notice: Undefined index: ... line 110

which is directed at this snippet:

$bVal += $values[substr(utf8_decode(mb_strtolower($line)),$j,1)];. 

Thing is, I have copied all files as is from the server including all the files it rely on. I only have to make small tweaks (Change the text files the php script reads from) but it won't even work with the original ones. Is there a common issue between platforms versions for PHP, Apache etc? As far as I can tell the XHTTP request is acting normally and I feel like I shouldn't be having these issues, this may come from my inexperience with PHP though.

Code will be posted below and thank you in advance for even taking a look at it!

Code: https://pastebin.com/M6y9rfRa

kourouma_coder
  • 1,078
  • 2
  • 13
  • 24
DCBN
  • 65
  • 2
  • 8
  • @miken32 thank you for your reply. As much as I appreciate you taking your time to answer the question, the articles linked do not explain why it would work on the server and not in my local environment (at least not as far as I understood) which is the real question. If I am wrong about that please do point it out with a specific quote. Yet again, thank you for your time! – DCBN Apr 02 '17 at 02:16
  • There could be any number of reasons, but the base of the problem is that `$values[whatever]` doesn't exist. First thing I would check is that you have multibyte functions installed. Also check the value that you're trying to use as an index. – miken32 Apr 02 '17 at 02:21
  • @DCBN Write the entire error. That normally helps... – JustCarty Apr 02 '17 at 02:31
  • Ah, you are right. I used ChromePhp to log my index as well as the result of indexing the array. Turns out that some of the indexing values were nothing and would result to null. I guess I have to read up on your articles about handling the issue itself though I must admit I feel that the incompatibility feels a bit weird. Thank you for your time sir! – DCBN Apr 02 '17 at 02:43

1 Answers1

0

Change your code to this and see how it goes

<script>
    function doSomething()
    {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function()
        {
            if (xhttp.readyState == 4)
            {
                if (xhttp.status == 200)
                    document.getElementById("calContent").innerHTML = xhttp.responseText;
            }
        }
        xhttp.open("GET", "/num/ns.php?number=" + jQuery("#nInput").val() + "$$$" + jQuery('input[name=switcher]:checked').val(), true);
        xhttp.send();
    }
    function doSomethingElse()
    {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function()
        {
            if (xhttp.readyState == 4)
            {
                if (xhttp.status == 200)
                    document.getElementById("calContent").innerHTML = xhttp.responseText;
            }
        }
        xhttp.open("GET", "/num/ns.php?number2=" + jQuery("#nInput2").val() + "$$$" + jQuery('input[name=switcher2]:checked').val(), true);
        xhttp.send();
    }
</script>
<style>
    .calOut {
        position: relative;
        float: left;
        margin-right: 18px;
    }
</style>
<style media="print">
    #calContent { display: none; }
</style>
<div id="calContent" style="margin-top: 0px; margin-bottom: 50px; color:black;" class="contact_form" >

<?php
    $numname = "number";
    if (isset($_GET["number"])) {
        echo "<div><input type='text' id='nInput' name='nInput' value='" . explode("$$$", $_GET["number"])[0] . "' />";
        echo "<br /><input type='radio' name='switcher' value='female' "; if (explode("$$$", $_GET["number"])[1] == "female") echo "checked"; echo ">Female&nbsp;&nbsp;";
        echo "<input type='radio' name='switcher' value='male' " . (explode("$$$", $_GET["number"])[1] == "male" ? "checked" : "") . ">Male&nbsp;&nbsp;";
        echo "<input type='radio' name='switcher' value='last' " . (explode("$$$", $_GET["number"])[1] == "last" ? "checked" : "") . ">Last name&nbsp;&nbsp;";
    }
    else {
        echo "<div><input placeholder='Enter vibration' type='text' id='nInput' name='nInput' />";
        echo "<br /><input type='radio' name='switcher' value='female' checked>Female&nbsp;&nbsp;<input type='radio' name='switcher' value='male' >Male&nbsp;&nbsp;";
        echo "<input type='radio' name='switcher' value='last' />Last name";
    }
    echo "<br /><input type='submit' id='nForm' value='Generate names' onclick='doSomething()' class='qbutton' style='margin-bottom: 45px;'/><br /></div>";

    if (isset($_GET["number2"])) {
        echo "<div><input type='text' id='nInput2' name='nInput2' value='" . explode("$$$", $_GET["number2"])[0] . "' />";
        echo "<br /><input type='radio' name='switcher2' value='female' "; if (explode("$$$", $_GET["number2"])[1] == "female") echo "checked"; echo ">Female&nbsp;&nbsp;";
        echo "<input type='radio' name='switcher2' value='male' " . (explode("$$$", $_GET["number2"])[1] == "male" ? "checked" : "") . ">Male&nbsp;&nbsp;";
        echo "<input type='radio' name='switcher2' value='last' " . (explode("$$$", $_GET["number2"])[1] == "last" ? "checked" : "") . ">Last name&nbsp;&nbsp;";
        $numname = "number2";
    }
    else {
        echo "<div><input placeholder='Enter vibration and number, eg. Lisa 17/8' type='text' id='nInput2' name='nInput' />";
        echo "<br /><input type='radio' name='switcher2' value='female' checked>Female&nbsp;&nbsp;<input type='radio' name='switcher2' value='male' >Male&nbsp;&nbsp;";
        echo "<input type='radio' name='switcher2' value='last' />Last name";
    }

    echo "<br /><input type='submit' id='nForm2' value='Generate names' onclick='doSomethingElse()' class='qbutton' /><br /></div>";

    //echo $_SERVER['HTTP_REFERER'];
    //echo "<br />";
    //if ($_SERVER['REMOTE_ADDR'] == "93.176.86.170" || $_SERVER['REMOTE_ADDR'] == "86.52.165.122")
    //  echo $_SERVER['REQUEST_URI'];

    $values = [
        "a" => 1, "i" => 1, "j" => 1, "q" => 1, "y" => 1, "å" => 1, "á" => 1, "à" => 1,
        "b" => 2, "k" => 2, "r" => 2,
        "c" => 3, "g" => 3, "l" => 3, "s" => 3, "ç" => 3,
        "d" => 4, "m" => 4, "t" => 4,
        "e" => 5, "h" => 5, "n" => 5, "x" => 5, "é" => 5, "è" => 5, "è" => "5",
        "u" => 6, "v" => 6, "w" => 6, "æ" => 6,
        "o" => 7, "z" => 7, "ø" => 7, "ô" => 7, "ó" => 7, "ò" => 7, "ö" => 7,
        "f" => 8, "p" => 8];
?>
<br />
<?php



//ini_set('memory_limit', '2048M');
error_reporting(0);
ini_set("display_errors", 0);
ini_set("log_errors", 0);
ini_set("error_reporting", 0);


    if (isset($_GET[$numname]))
    {
        $namecount = 0;
        if (is_numeric(explode("/", explode("$$$", $_GET["number"])[0])[0]))
        {
            echo "<br />";
            if (explode("$$$", $_GET[$numname])[1] == "male")
                $handle = fopen("./malenames.txt", "r");
            else if (explode("$$$", $_GET[$numname])[1] == "female")
                $handle = fopen("./femalenames.txt", "r");
            else
                $handle = fopen("./surnames.txt", "r");
            $output = Array();
            if ($handle)
            {
                while (($line = fgets($handle)) !== false)
                {
                    $bVal = 0;
                    for($j=0;$j<=strlen(utf8_decode($line));$j++)
                    {
                        $bVal += $values[substr(utf8_decode(mb_strtolower($line)),$j,1)];
                    }
                    if ($bVal==explode("/", explode("$$$", $_GET[$numname])[0])[0] || explode("/", explode("$$$", $_GET[$numname])[0])[0] == (1+(($bVal-1)%9)) )
                    {
                        //echo $line . "<br />";
                        $output[$namecount] = $line . " (" . $bVal . "/" . (1+(($bVal-1)%9)) . ")<br />";
                        $namecount++;
                    }
                    //echo $line . "<br />";
                }
                fclose($handle);
                echo "<p id='numcount'>Found " . $namecount . " names</p>";
                echo "<br />";
                for ($i=0;$i<count($output);$i++)
                    echo $output[$i];
                unset($output);
            }
        }
        else if (is_numeric(explode(" ", explode("/", explode("$$$", $_GET[$numname])[0])[0])[1]))
        {
            $parm1 = explode(" ", explode("$$$", $_GET[$numname])[0]);
            $oldname = utf8_decode($parm1[0]);
            $num = explode("/", $parm1[1])[0];
            //echo "<p>" . $parm1 . "</p>";
            //echo "<p>" . $oldname . "</p>";
            //echo "<p>" . $num . "</p>";
            echo "<br />";
            if (explode("$$$", $_GET[$numname])[1] == "male")
                $handle = fopen("./malenames.txt", "r");
            else if (explode("$$$", $_GET[$numname])[1] == "female")
                $handle = fopen("./femalenames.txt", "r");
            else
                $handle = fopen("./surnames.txt", "r");
            $output = Array();
            if ($handle)
            {
                while (($line = fgets($handle)) !== false)
                {
                    $linex = $parm1[0] . mb_strtolower($line);//utf8_decode(mb_strtolower($oldname . mb_strtolower($line)));
                    $bVal = 0;
                    for($j=0;$j<=strlen(utf8_decode($linex));$j++)
                    {
                        $bVal += $values[substr(utf8_decode(mb_strtolower($linex)),$j,1)];
                    }
                    if ($bVal==$num || $num == (1+(($bVal-1)%9)) )
                    {
                        //echo $line . "<br />";
                        $output[$namecount] = $linex . " (" . $bVal . "/" . (1+(($bVal-1)%9)) . ")<br />";
                        $namecount++;
                    }
                    //echo $line . "<br />";
                }
                fclose($handle);
                echo "<p id='numcount'>Found " . $namecount . " names</p>";
                echo "<br />";
                for ($i=0;$i<count($output);$i++)
                    echo $output[$i];
                unset($output);
            }
        }
        else
        {
            echo "<br />Error in input";
        }
        echo "<div style='clear:both;'></div>";
    }
?>
</div>
<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
Otávio Barreto
  • 1,536
  • 3
  • 16
  • 35
  • Hey! The thing is, we are inside the ns.php file. It is sending a xhttp request to itself so that the php script can get the input data out of the url. I also monitored the request in the network tab (In Chrome). No errors and the request seems to be formatted properly! – DCBN Apr 02 '17 at 03:04
  • please show me more info what the **Undefined index name** shows? it's often caused by a error of '' " – Otávio Barreto Apr 02 '17 at 03:06
  • Basically, I get errors for every "index" that is supposed to get printed. I just printscreened the error and it repeat itself X times, I am not sure exactly how many times since I have not counted but it is a lot. Image: http://puu.sh/v6okb/2716b07de1.png – DCBN Apr 02 '17 at 03:13
  • WOW! It worked, what exactly was changed. I can see the code but I dont quiet understand the syntax. Thank you a lot! – DCBN Apr 02 '17 at 03:16
  • Your code was working fine, the error part was just the WAMP server configuration, so I just edited your php and created a script to set the php ini to ignore these alerts on this page here is the magic **ini_set("display_errors", 0); ini_set("log_errors", 0); ini_set("error_reporting", 0);** It means it will hide these erros for this script because your php.ini file was showing all error, it will show in others files but for this script it will hide. Don't you worry the problem was not with your script, it was the wampp server configuration in the **php.ini** file – Otávio Barreto Apr 02 '17 at 03:20
  • 1
    Ah okay, then I understand. Weird it would spit out these errors, since it is already working on the server I assume I can just re-upload with the "hiding" commented out since it isn't triggered on the server and then save the snippet when developing? Anyways, thanks a bunch! – DCBN Apr 02 '17 at 03:23
  • Yes if you add a comment in this part of the code it will show you in your development, but It's very usefull too for security reasons, and improve your website view because it will hide info for script kiddies and the users of your site will not be seeing script errors – Otávio Barreto Apr 02 '17 at 03:30
  • Yeah! You are right, might as well keep it and reverse whatever I said :D – DCBN Apr 02 '17 at 03:41