I wrote this script for a link shortener just for fun.
Is it safe, or is it very easy to inject SQL have other security shortcomings? Also, do you have any tips against MySQL injection?
I don't know how to make it better. I don't know, but it tells me that this is mostly code, and I should add some details but I don't know what to write!
<?
$user = check_user();
$uid = $user["id"];
$uid2 = $_SESSION['userid'];
$link1 = $_POST['link1'];
$link2 = $_POST['link2'];
$flink = $_POST['url'];
$action = $_POST['action'];
$action1 = $_POST['action1'];
$action2 = $_POST['action2'];
$faction = $_POST['faction'];
$adgrade = $_POST['adgrade'];
$monetize = $_POST['monetize'];
// $re = '/^(?:https?\:\/\/)?[a-z0-9\-_]+\.[a-z0-9\-_\.\/%&=\?\+]+$/mi';
$re2 = '/^[a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+$/i';
if (isset($link1) && isset($link2) && isset($flink) && !empty($link1) && !empty($link2) && !empty($flink)) {
if (!preg_match("~^(?:f|ht)tps?://~i", $link1)) {
$link1 = "http://" . $link1;
}
if (!preg_match("~^(?:f|ht)tps?://~i", $link2)) {
$link2 = "http://" . $link2;
}
if (!preg_match("~^(?:f|ht)tps?://~i", $flink)) {
$flink = "http://" . $flink;
}
}
if (isset($link1) && isset($link2) && isset($flink) && !empty($link1) && !empty($link2) && !empty($flink)) {
if (preg_match($re2, $flink, $matches, PREG_OFFSET_CAPTURE, 0) && preg_match($re2, $link1, $matches, PREG_OFFSET_CAPTURE, 0) && preg_match($re2, $link2, $matches, PREG_OFFSET_CAPTURE, 0)) {
if (isset($action) && isset($action1) && isset($action2) && isset($faction)) {
//$id = substr(md5(time().$flink), 0, 5);
$id = substr(str_shuffle(str_repeat("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxYyYzZ", 5)), 0, 5);
if ($conn->query("INSERT INTO `" . $database . "`.`link` (`id`, `link1`,`link2`,`action`,`action1`,`action2`,`faction`, `flink`,`adonly`,`userid`,`adgrade`,`monetize`) VALUES ('" . $id . "','" . $link1 . "','" . $link2 . "','" . $action . "','" . $action1 . "','" . $action2 . "','" . $faction . "', '" . $flink . "','false','" . $uid . "','" . $adgrade . "','" . $monetize . "');")) {
$html = 'Your STU-Link is <a class="test" href="https://example.com/' . $id . '">https://example.com/' . $id . '</a>';
} else {
$error_sth_wrong = "Something went wrong!";
}
} else {
$error_sl_actions = "Please select all actions!";
}
mysqli_close($db);
} else {
$error_iv_urls = "Invalid url(s)!";
}
} else {
$error_iv_urls2 = "Invalid url(s)!";
}
?>
empty()
checks forisset()
internally. don't need to manually check if it's set, just check if it's empty. \$\endgroup\$ – I wrestled a bear once. Jul 19 '17 at 19:52