r/PHPhelp • u/netgr115 • Feb 15 '25
how to fix this code ?
<?php
require_once('./include/functions.php');
//require_once('./include/users.functions.php');
dbconn(true);
global $CURUSER, $TABLE_PREFIX, $btit_settings;
// Check if the user is logged in and has permission to view the page
if (!$CURUSER || $CURUSER["view_users"] != "yes") {
die('<center><br><br>Access Denied</center>');
}
// Initialize message variable
$message = '';
// Process form submission
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['wishsend'])){
// Validate and sanitize inputs
$wishtitle = htmlspecialchars($_POST['wishtitle'], ENT_QUOTES, 'UTF-8');
$wishcomment = htmlspecialchars($_POST['wishcomment'], ENT_QUOTES, 'UTF-8');
$wishgenre = htmlspecialchars($_POST['wishgenre'], ENT_QUOTES, 'UTF-8');
// Prepare user's name with color formatting
$wishname = $CURUSER["prefixcolor"] . $CURUSER["username"] . $CURUSER["suffixcolor"];
$nick = $CURUSER["username"];
$color = user_with_color($nick);
$color = explode("#", $color)[1];
$color = "#" . substr($color, 0, 6);
$wishnamechat = "[color=$color]{$CURUSER['username']}[/color]";
// Insert wish into the database
$wishsql = "INSERT INTO {$TABLE_PREFIX}radio_wish (name, title, comment, genre, date)
VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($GLOBALS["___mysqli_ston"], $wishsql);
mysqli_stmt_bind_param($stmt, 'ssssi', $wishname, $wishtitle, $wishcomment, $wishgenre, time());
mysqli_stmt_execute($stmt) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
mysqli_stmt_close($stmt);
// Insert notification into the chat
$chatbox = "INSERT INTO {$TABLE_PREFIX}chat (uid, time, name, text)
VALUES (0, ?, 'System', ?)";
$stmt = mysqli_prepare($GLOBALS["___mysqli_ston"], $chatbox);
$chatText = "$wishtitle - $wishcomment - $wishgenre by $wishnamechat";
mysqli_stmt_bind_param($stmt, 'is', time(), $chatText);
mysqli_stmt_execute($stmt) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
mysqli_stmt_close($stmt);
$message = "<font color='silver'>Your request has been submitted to the DJ's.</font>";
}
// Handle wish deletion
if (isset($_GET['delete']) && is_numeric($_GET['delete'])) {
$id = intval($_GET['delete']);
$wishsql = "DELETE FROM {$TABLE_PREFIX}radio_wish WHERE id = ?";
$stmt = mysqli_prepare($GLOBALS["___mysqli_ston"], $wishsql);
mysqli_stmt_bind_param($stmt, 'i', $id);
mysqli_stmt_execute($stmt) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
mysqli_stmt_close($stmt);
}
// Fetch the latest wishes
$wishsql = "SELECT * FROM {$TABLE_PREFIX}radio_wish ORDER BY date DESC LIMIT 10";
$wishresult = mysqli_query($GLOBALS["___mysqli_ston"], $wishsql) or die(mysqli_error($GLOBALS["___mysqli_ston"]));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Radio Wishlist</title>
<link rel="stylesheet" type="text/css" href="<?php echo $STYLEURL; ?>/main.css">
</head>
<body>
<center>
<?php echo $message; ?>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
<table width="100%" border="0">
<tr>
<td class="header" width="15%">Artist:</td>
<td class="header" width="15%"><input type="text" name="wishtitle" required></td>
<td class="header" width="15%">Title:</td>
<td class="header" width="15%"><input type="text" name="wishcomment" required></td>
<td class="header" width="15%">Genre:</td>
<td class="header" width="15%"><input type="text" name="wishgenre" required></td>
<td class="header" width="15%">
<input type="hidden" name="wishsend" value="wishsend">
<input type="submit" name="submit" value="Post">
</td>
</tr>
</table>
</form>
<br>
<table border="0">
<tr>
<th class="header" width="20%">User:</th>
<th class="header" width="20%">Artist:</th>
<th class="header" width="20%">Title:</th>
<th class="header" width="20%">Genre:</th>
<th class="header" width="20%">Date and Time</th>
<?php if ($CURUSER["admin_access"] == "yes"): ?>
<th class="header" width="10%">Action</th>
<?php endif; ?>
</tr>
<?php while ($wishes = mysqli_fetch_assoc($wishresult)): ?>
<tr>
<td class="lista"><?php echo $wishes['name']; ?></td>
<td class="lista"><?php echo $wishes['title']; ?></td>
<td class="lista"><?php echo nl2br($wishes['comment']); ?></td>
<td class="lista"><?php echo nl2br($wishes['genre']); ?></td>
<td class="lista"><?php echo date('d-m-Y H:i:s', $wishes['date']); ?></td>
<?php if ($CURUSER["admin_access"] == "yes"): ?>
<td class="lista"><a href="<?php echo $_SERVER['PHP_SELF']; ?>?delete=<?php echo $wishes['ID']; ?>">Remove</a></td>
<?php endif; ?>
</tr>
<?php endwhile; ?>
</table>
</center>
</body>
</html>
View all comments
Show parent comment
1
u/equilni Feb 16 '25
PHP:
Pluses!
a) Prepared statements!!!
b)
===Strict comparisons!c) Escaping with
htmlspecialchars(not in the right place though... see below)Minuses:
a) require_once doesn't need parenthesis. I prefer none.
https://www.php.net/manual/en/function.include.php
b) $GLOBALS and globals are frowned upon. Your database and user are in globals.... ugh.
c)
Validate and sanitize inputs. There's no validation going on.htmlspecialcharsis for output, not input. You can also wraphtmlspecialchars(string, ENT_QUOTES, 'UTF-8')in a function to not duplicate code.d) Follow this tutorial for mysqli.
e) There's no validation on the delete side of things. Also
ID=/= 'id`, so I would make sure error reporting is on.f)
$STYLEURLisn't defined in your globals.g)
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>wouldn't be needed if you didn't do things like mysite.com/chat.php. You also don't do this later on with the deletehref="<?php echo $_SERVER['PHP_SELF'];h) You use the {} syntax, but missed using it here -
$chatText = "$wishtitle - $wishcomment - $wishgenre by $wishnamechat";There's likely more, but I don't have time to go through it all. I would highly suggest breaking this into a MVC like structure, remove globals and learn to use classes.