-1

My goal: compare the actual url to a stored url in MySQL DB. If there's a match the page title and meta description info is loaded from the DB into the fields on the page.

Problem: I need to remove the query string so the url is clean for the comparison.

The existing code works for the main pages that list the blogs/lessons/etc, but when I go to page 2, 3, 4, etc the query string breaks the comparison.

In the code you will see $actual_link - that needs to have the query string removed and that is what I have yet to find a solution that actually works, as for what I've tried, there have been too many iterations to remember them.

Server Info: PHP 7.3.17; MySQL MariaDB-10.2.32; Apache-2.4.43/

$actual_link = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$sql = "SELECT * FROM meta WHERE siteLink = '$actual_link'";
$result = mysqli_query($link, $sql);
if (mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
?>
<?php include_once "myHead.php"; ?>
<title><?php echo $row["title"]; ?></title>
<meta name="description" content="<?php echo $row["meta_desc_content"]; ?>">
<?php
}
}
else
{
echo "Shit, it ain't working.";
}
?>
ChipW
  • 3
  • 1
  • you should take a look at prepared statement and bindiing param – ScaisEdge Jun 15 '20 at 18:20
  • Thanks for the comment. Yeah, I'm aware of prepared statements, but for me learning something new is difficult and takes much time and repetition/use (it's a learning disability). But I'm working on it (getting my head wrapped around them). – ChipW Jun 15 '20 at 21:44

2 Answers2

1

PHP provides a nice helper method called parse_url to get certain parts of an url:

// $actual_link = 'www.stackoverflow.com/test';
$urlParts = parse_url($actual_link);
$formattedUrl = $urlParts['host']; // www.stackoverflow.com

Docs: https://www.php.net/manual/en/function.parse-url.php

MaartenDev
  • 5,631
  • 5
  • 21
  • 33
0

Use strtok()

like this: $baseUrl = strtok($actual_link, '?');

musashii
  • 445
  • 6
  • 13