Prime
Prime

Reputation: 3625

Why $_SERVER['REQUEST_URI' NOT working with IF condition

My webpage url is http://example.com/blog/

When I echo it is showing the correct result as blog.

echo $_SERVER['REQUEST_URI']

>>blog

But, when I use in if condition

<?php if($_SERVER['REQUEST_URI']== 'blog') echo 'class="Margincls"';?>

it doesn't return anything, tried with basename and strtolower as well

Upvotes: 0

Views: 5995

Answers (5)

Arman H
Arman H

Reputation: 1754

I think your site is live. if you use only $_SERVER['REQUEST_URI'] it will work only in localhost.

You can try this.

  if (strpos($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], '/blog/') == true){
         echo "Hello world";
    }else{
        echo "Hello WOrld 2";
    }

Upvotes: 0

troshan
troshan

Reputation: 140

This should work

<?php if($_SERVER['REQUEST_URI']== '/blog/') echo 'class="Margincls"';?>

Upvotes: 0

$_SERVER['REQUEST_URI'] also returns the first forward slash, so you need to check this when equating.

<?php if($_SERVER['REQUEST_URI']== '/blog') echo 'class="Margincls"';?>

Upvotes: 0

Nghi Ho
Nghi Ho

Reputation: 463

If your url is http://example.com/blog/, $_SERVER['REQUEST_URI'] should be /blog/. I checked on my environment - Apache, PHP 5.6.

Reference: http://php.net/manual/en/reserved.variables.server.php

Upvotes: 1

Elisha Senoo
Elisha Senoo

Reputation: 3594

You are using wrong quotations. Copy and paste this:

<?php if($_SERVER['REQUEST_URI']== 'blog') echo 'class="Margincls"';?>

Upvotes: 0

Related Questions