Jump to content

Check url


antdk

Recommended Posts

Hi,

 

I have this string:

<?php $url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];?>

If i print this, i get something like http://xxxxx.dk/foo/foo/43 http://xxxxx.dk/foo/foo/54 etc

 

Now i only want a specific event to orrur when the last number in the url is a specifc number like 1, 2, 3 or 4). How can this be achieved?

 

Its important to say that the "foo"'s can be variable. So i one url it could be abc/qqq/123 and another hi/mom/9823 . So there might need somekind of "all" code as a replacement for the foos?

 

 

Link to comment
Share on other sites

You confuse yourself methinks.  You said that you only care about the last digit, then you worry about some "foo" values which do not appear to be the last digit.

 

This is simple string manipulation which I'm sure you have read about in the php manual.  And if not, I suggest that be your next stop.  (It should have been your first stop....)

Link to comment
Share on other sites

Going by what you asked, this can do what you need.

 

 

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

$last_path = end(explode("/", rtrim($url, "/")));

if (!is_int($last_path) && $last_path < 1) {
    $last_path = 1;
}

switch ($last_path) {
    case 1:
        echo "Number one";
        break;
    case 2:
        echo "Number two";
        break;
    case 3:
        echo "Number three";
        break;
    case 4:
        echo "Number four";
        break;
}
 

 

Did you consider using GET requests in your urls?

http://xxxxx.dk/foo/foo/?page=4

 

 

if(isset($_GET['page']) && trim($_GET['page']) != '' && ctype_digit($_GET['page']) && $_GET['page'] >= 1){
$page = trim($_GET['page']);
} else {
$page = 1;
}
echo $page;
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.