Here we go again, I need some help with this.
the preg_match is not working as I want it, it is not validating any of site links. I need a 2nd pair of eyes to help me see what is wrong with my code.
if (!empty($_POST["url"]))
{
if (filter_var($_POST["url"], FILTER_VALIDATE_URL))
{
if (!preg_match('/^http(s)?:\/\/(?:[a-z\d-]+\.)*mysite.com(?:(?=\/)|$)/i', $url))
{
echo "<strong>Error</strong>: Not a valid Mysite.com link or could shorten link";
} else {
$result = $sql->query("SELECT `id` FROM `shortcuts` WHERE `url`='{$_POST["url"]}'");
$id = $result[0]["id"];
if (empty($id))
{
$result = $sql->query("INSERT INTO `shortcuts` (`url`) VALUES ('{$_POST["url"]}')");
if ($result)
{
$id = $sql->get_increment();
if (empty($id))
{
echo "FAILED ENCODE";
exit(1);
}
}
$shorturl = "http://mysite.com/".encode($id);
}
}
}
}
Make yourself the code less complicated, so it is easier to debug. You are concerned about a specific problem here if I understood you right:
Create yourself a function. Good thing about is, you can test it isolated without submitting something or hitting the database.
It’s much easier to test this function alone with the inputs you want to test it again. Also you’re properly checking for error conditions then. Good luck.