2

I want to check if my current URL contains "/demo" at the end of the url, for example mysite.com/test/somelink/demo to do something. Here is my attempt :

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if($host == 'mysite.com/test/somelink/demo') 
 {
// Do something
}
else
{
// Do something
}

This seems to work fine, but the problem is that /somelink needs to by dynamic.

Any suggestion on how can I do this ?

Thank you !

Edit:

<?php
/* An abstract class for providing form types */
abstract class ECF_Field_Type {
    private static $types = array();
    protected $name;

    /* Constructor */
    public function __construct() {
        self::register_type( $this->name, $this );
    }

if(basename($_SERVER['REQUEST_URI']) == 'stats'){
echo "Hello World";
}

    /* Display form field */

    public abstract function form_field( $name, $field );

    /* Display the field's content */
    public function display_field( $id, $name, $value ) {
        return "<span class='ecf-field ecf-field-$id'>"
            . "<strong class='ecf-question'>$name:</strong>"
            . " <span class='ecf-answer'>$value</span></span>\n";
    }


    /* Display field plain text suitable for email display */
    public function display_plaintext_field( $name, $value ) {
        return "$name: $value";
    }

    /* Get the description */
    abstract public function get_description();
}
?>
agis
  • 1,831
  • 10
  • 33
  • 68

5 Answers5

4

Just use,

if(basename($_SERVER['REQUEST_URI']) == 'demo'){
    // Do something
}
Rikesh
  • 26,156
  • 14
  • 79
  • 87
  • Just tried but I'm getting a weird syntax error : `Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in` – agis Jan 30 '14 at 13:52
  • Yes, I think is something wrong with my code but I don't understand why, I've edited my question with what I've tried please take a look... – agis Jan 30 '14 at 14:02
  • So now what is the issue ? – Rikesh Jan 30 '14 at 14:07
  • `Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in` – agis Jan 30 '14 at 14:24
  • And if put it outside the `abstract class ECF_Field_Type {` is working fine – agis Jan 30 '14 at 14:25
  • I think I will ask another question for this issue because is not related with what I was asking for. Thank you ! – agis Jan 30 '14 at 14:49
1
<?php
    if (preg_match("/\/demo$/", $_SERVER['REQUEST_URI'])) {
        // Do something
    } else {
        // Do something else
    }
?>
MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • Just tried it and Im getting a weird syntax error: `Parse error: syntax error, unexpected T_IF, expecting T_FUNCTION in` – agis Jan 30 '14 at 13:56
0

This post has PHP code for simple startsWidth() and endsWith() functions in PHP that you could use. Your code would end up looking like:

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(endsWith($host, '/demo'))
{
// Do something
}
else
{
// Do something
}

But one thing you might want to do in addition to that is convert $host to lowercase so the case of the URL wouldn't matter. EDIT: That would end up looking like this:

$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if(endsWith(strtolower($host), '/demo'))
Community
  • 1
  • 1
mystiscool
  • 141
  • 4
0

you can use strpos

$host = 'mysite.com/test/somelink/demo';
if(strpos($host,'demo')) 
  {
// Do something 
   echo "in Demo";
}
else
{
// Do something
 echo "not in Demo";
}
Maz I
  • 3,664
  • 2
  • 23
  • 38
0
$str = 'demo';
if (substr($url, (-1 * strlen($str))) === $str) { /**/ }
u_mulder
  • 54,101
  • 5
  • 48
  • 64