Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP nested menu security question

From a novice:

In looking to display a modified nested menu of How to create a nested menu from MySQL with PHP?. My question is: are there any security concerns in taking this kind of approach. From my novice point of view this code is server-sided with the exception of triggering the query upon the page being loaded.

All insights and suggestions are welcome. Thank you.

<?php
include '../data.php'; // connection folder  

$query = "SELECT `parent_name`, `parent_id`, `child_name`, child_id
            FROM  `pages.child` INNER JOIN `pages.parent`
            ORDER BY `parent_name`";

$result = mysql_query($query) or die(mysql_error());
echo "<ul id=\"catmenu\">";
$last_parent = '';
while($row = mysql_fetch_array($result)){
    if($last_parent != $row['parent_name']){
            // Unless this is the first item, close the last category
            if($last_parent != ''){
                    echo "</ul></li>";
            }
            // Parent menu begins <li> and <ul>
            $last_parent = $row['parent_name'];
            $tags = $row['parent_name'];               
            echo "<a href=\"$tags\"><li class=\"menulist\">{$tags}<ul></a>";
    }
    if($row['parent_id'] === $row['child_id'] ){
        $tags = $row['parent_name'];
        $tag = $row['child_name'];
        echo "<li class=\"menulist\"><a href=\"$tags\\$tag\">$tag</a>";
        }        
}
if($last_parent != ''){
    echo "</ul></li>";
}
echo "</ul>";

?>
like image 957
crash_course Avatar asked Dec 08 '25 03:12

crash_course


1 Answers

If the values in the database table were previously inputted by users, make sure you escape them using htmlentities() before outputting them. For example, replace the line:

$tags = $row['parent_name'];

With this:

$tags = htmlentities($row['parent_name']);

Using htmlentities() prevents a vulnerability known as cross-site scripting, which is the only security issue I can see in this situation.

like image 98
EdoDodo Avatar answered Dec 10 '25 16:12

EdoDodo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!