OneEightLeft
OneEightLeft

Reputation: 324

Trying to display a title based on the url in PHP

So i'm putting together a simple php page which will display different content based on what url is put in. For some reason it seems to make it through the whole if statement without doing anything.

<?php
    if(array_key_exists('offer', $_GET)) {
        if( $_GET == "ncp" ){
            $title = 'Exclusive Offer from NCP';
        } elseif($_GET == "rt") {
            $title = 'Exclusive Offer from RT';
        } elseif($_GET == "oo") {
            $title = 'Exclusive Offer from OO';
        }
    } else {
        $title = 'Check Out These Exclusive Offers!';
    }
    $title = strip_tags($title);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title; ?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script type="text/javascript">

</script>
</head>

Upvotes: 0

Views: 162

Answers (3)

Joren
Joren

Reputation: 3297

You should use $_GET['offer'] == "foo" instead of $_GET == "foo"

Better yet, use strict comparison in the future to make sure you are comparing the same types (in this case strings): ===. This is because PHP has the tendency to cast types around, which can be both useful and annoying (see http://php.net/manual/en/language.types.type-juggling.php):

$foo = "0";  // $foo is string (ASCII 48)
$foo += 2;   // $foo is now an integer (2)
$foo = $foo + 1.3;  // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs";     // $foo is integer (15)

$_GET: http://php.net/manual/en/reserved.variables.get.php

comparison operators: http://php.net/manual/en/language.operators.comparison.php

Upvotes: 6

user1279047
user1279047

Reputation: 1421

Another way of doing it Try this I always make it a point to clean any variable passed via $_POST, $_GET or any external source thats why I use filter_var.

<?php

    if(isset($_GET['offer'])) {     
      //lets sanitize $_GET['offer'] if it exist since its passed via url
      $offer = filter_var($_GET['offer'], FILTER_SANITIZE_STRING);
    } else {
      $offer = NULL;
    }

    switch ($offer) {
    case 'ncp':
      $title = 'Exclusive Offer from NCP';
    break;
    case 'rt':
      $title = 'Exclusive Offer from RT';
    break;
    case 'oo':
      $title = 'Exclusive Offer from OO';
    break;
    default:
      $title = 'Check Out These Exclusive Offers!';
    break;
    }

?>

Upvotes: 1

s.d
s.d

Reputation: 255

This is how you can use $_GET in php. Give it a try

<?php
if(isset($_GET['offer'])){
    if( $_GET['offer'] == "ncp" ){
    $title = 'Exclusive Offer from NCP';
    }
    elseif( $_GET['offer'] == "rt" ){
    $title = 'Exclusive Offer from RT';
    }
    elseif( $_GET['offer'] == "oo" ){
    $title = 'Exclusive Offer from OO';
    }
} 
else {
    $title = 'Check Out These Exclusive Offers!';
}
$title = strip_tags($title);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title><?php echo $title; ?></title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>
<script type="text/javascript">
</script>
</head>

Upvotes: 0

Related Questions