1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Using $_SERVER['REQUEST-URI']

Discussion in 'PHP' started by jacobbannier, Apr 2, 2009.

  1. #1
    Hi, I have a script which needs to know if a certian term is in the URL.
    I used $_SERVER['REQUEST_URI'] to get the URL, but this is adding higher directories, and I don't want this as it makes the $_SERVER['REQUEST_URI'] different to my string.
    PHP SCRIPT:
    <?php
    
     $page = $_SERVER['REQUEST_URI'];
    if ($page=="/series1/episode2.php");
           {
     	$url='episode2';
     	}
    
    
    ?>
    Code (markup):
    2 possible options to solve this:

    • Cutting out the unwanted part of the URL - Using the $_SERVER['REQUEST_URI'] function outputs /Final%20WTIO/series1/episode2.php, where I only want it to output //series1/episode2.php[/i]. Is there a way to cut out the higher directories?
    • Option 2 - Using an 'in string function'. Is there a function that looks in the string and finds a certain (predetermined) phrase? This would be used, ie.

    Thanks for your help, if you understand the longwinded problem :)
     
    jacobbannier, Apr 2, 2009 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    This should be the fastest way to check. There are other function like preg_match but strpos is the fastest.

    if(strpos($_SERVER['REQUEST_URI'],'episode1') !== false){

    //DO STUFF

    }
     
    jestep, Apr 2, 2009 IP
    jacobbannier likes this.
  3. jacobbannier

    jacobbannier Active Member

    Messages:
    1,155
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    90
    #3
    Thank you my friend, with a few minor tweaks this now does exactly what I wanted! Repped :)
     
    jacobbannier, Apr 2, 2009 IP
  4. jacobbannier

    jacobbannier Active Member

    Messages:
    1,155
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Sorry, double post (hit submit twice) :(
    To make it worth while I suppose I'll post the code that worked for me:
    	mysql_connect("localhost","","");
    					mysql_select_db("inbetweeners");
    	if(strpos($_SERVER['REQUEST_URI'],'series1/episode2') == true){
    	 $url = 'series1/episode2';
            } elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode3') == true) {
    	$url = 'series1/episode3';
    	}	
            elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode4') == true) {
    	$url = 'series1/episode4';
    	}
    	elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode5') == true) {
    	$url = 'series1/episode5';
    	}
    	elseif(strpos($_SERVER['REQUEST_URI'],'series1/episode6') == true) {
    	$url = 'series1/episode6';
    	}
    Code (markup):
     
    jacobbannier, Apr 2, 2009 IP
  5. rosseric

    rosseric Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    using strpos isn't very smart, as /foo/episode1/bar, /view/episode1/taz... will all return the same thing.
    it can also be used to harm your site (getting 1000's of pages indexed with duplicate content under your domain isn't much helpful for your ranks)

    if you want to remove the first part of the path, try
    
    $path = $_SERVER['REQUEST_URI'];
    $path = substr($path, strpos($path, '/', 1) + 1);
    
    Code (markup):
    a request to hxttp://www.foo.com/bar/taz/baz will get "taz/baz" in $path
     
    rosseric, Apr 2, 2009 IP
    jacobbannier likes this.
  6. jacobbannier

    jacobbannier Active Member

    Messages:
    1,155
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    90
    #6
    Thanks I will try this also, this is more what I wanted :)
    Repped too
     
    jacobbannier, Apr 3, 2009 IP