maarcs
maarcs

Reputation: 115

Different page title for each page using only one header

I a building a blog. I want one single title for the index page and different titles for each post page. So I made this kind of code. Is this the best way to do that?

<title><?php 
    $posttitle = $_GET["article"];

    if (empty($posttitle)) {
print $blog_title;
    }

    else {
        $result = mysqli_query($con,"SELECT * FROM post WHERE postlink='$posttitle' ");
        while($row = mysqli_fetch_array($result))

                {
                    print $row['title'];
                };
        };


    ?></title>

This code works for me. But I dont think its good idea to check if we are viewing an article every time.

Upvotes: 0

Views: 144

Answers (1)

imclickingmaniac
imclickingmaniac

Reputation: 1563

First:

 $posttitle = $_GET["article"];

    if (empty($posttitle)) {
      print $blog_title;
    }

will generate E_NOTICE error if $)GET is not set. Do:

  if (empty($_GET['article'])) {
     $posttitle = $_GET['article'];
      print $blog_title;
    }

also use ' instead of " if not parsing strings.

Second: escape everything what is get from POST or GET! if you expect data to be article id, do the (ing $_GET['article']) and read about SQL injection.

Upvotes: 1

Related Questions