SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast indepthmedia's Avatar
    Join Date
    Apr 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirecting a page to itself minus a query string

    Ok, I have started the following code but I don't know how to strip out the query string...

    PHP Code:
    <?php $query $_GET['query'];
    $oldurl = echo $_SERVER['PHP_SELF']; //For example: http://www.mysite.com/currentpage.php?query=whatever
    if ($query != '') { 
        
    $newurl $oldurl//BUT REMOVING THE QUERYSTRING - How do I do this?
        
    Header"HTTP/1.1 301 Moved Permanently" ); 
        
    Header"Location: $newurl); 
    }    
    ?>

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use the parse_url() function . Documentation @ :
    http://www.php.net/manual/en/function.parse-url.php
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Enthusiast indepthmedia's Avatar
    Join Date
    Apr 2005
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you, but what if I have 2 parameters in the url but I only want to remove 1?

    For ex. http://www.mysite.com/currentpage.ph...2=whateverelse

    but I only wanted to strip out "query=whatever"

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Same function, just retrieve the query array. Concatenate the path and 1st query parameter and you are in business again.
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    header
    ('content-type: text/plain');

    $s 'http://example.com/path/to/file.ext?query=a&query2=b&mew=mew';
    list (
    $s$q) = explode('?'$s);
    parse_str($q$q);

    unset(
    $q['query']);

    # New Url:
    print $s '?' http_build_query($q'''&');
    # http://example.com/path/to/file.ext?query2=b&mew=mew

    ?>
    Weeeeeee
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2006
    Location
    /USA/Kentucky/Richmond/
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        /**
         * Retrieve $_GET params filtered by string or array exclusion list
         * @author Bobby Easland 
         * @version 1.0
         * @param mixed $exclude Exclusion list as either string or array
         * @param boolean $asString Flag to either return as string or array
         * @return mixed
         */    
        
    function getParamsFiltered($exclude$asString true){
            if ( empty(
    $_GET) ){
                return ( 
    $asString === true ) ? '' : array();
            }
            switch(
    gettype($exclude)){
                case 
    'string':
                    if ( !
    array_key_exists($exclude$_GET) ){
                        return ( 
    $asString === true ) ? implodeAssoc($_GET) : $_GET
                    }
                    
    $return = array();
                    foreach ( 
    array_keys($_GET) as $key ){
                        if ( 
    $key == $exclude ) continue;
                        
    $return[$key] = $_GET[$key];
                    }
                    if ( 
    count($return) < ){
                        return ( 
    $asString === true ) ? '' : array();
                    }
                    return ( 
    $asString === true ) ? implodeAssoc($return) : $return;
                    break;
                case 
    'array':
                    
    $diff array_diff(array_keys($_GET), $exclude);
                    if ( 
    count($diff) < ){
                        return ( 
    $asString === true ) ? '' : array();
                    }
                    
    $return = array();
                    foreach( 
    array_values($diff) as $key ){
                        
    $return[$key] = $_GET[$key];
                    }
                    return ( 
    $asString === true ) ? implodeAssoc($return) : $return;
                    break;
                default:
                    return ( 
    $asString === true ) ? '' : array();
                    break;
            }                
        } 
    # end function

        /**
         * Implode array associatively, back port of simple array http_build_query
         * @author Bobby Easland 
         * @version 1.0
         * @param array $array Associative array to be imploded
         * @param string $innerGlue Inner glue
         * @param string $outerGlue Outer glue
         * @return string
         */    
        
    function implodeAssoc($array$innerGlue='='$outerGlue='&') {
            if ( !
    is_array($array) ){
                return 
    '';
            }
            
    $output = array();
            foreach( 
    $array as $key => $item ){
                
    $output[] = trim($key) . $innerGlue trim($item);
            }
            return 
    implode($outerGlue$output);
        } 
    # end function 
    To use the code save the functions in either a library or other common included file. Here is an example of use:

    PHP Code:
        /**
         * Test URL = http://www.domain.com/script.php?var1=123&var2=456&var3=789
         */
         
        // String input, single filter
        
    echo getParamsFiltered('var1'); // Outputs var2=456&var3=789
        
        // Array input, single filter
        
    echo getParamsFiltered(array('var1')); // Outputs var2=456&var3=789
        
        // Array input, multiple filters
        
    echo getParamsFiltered(array('var1''var3')); // Outputs var2=456 
    Enjoy!

    Bobby
    Last edited by Chemo; Aug 18, 2007 at 07:52. Reason: Move test URL into code block

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Interesting function Chemo but it is not very scalable being restricted to $_GET only and a little too complex. Not to mention not so good on performance.

    PHP Code:
    <?php

    header
    ('content-type: text/plain');

    # ($sourceArray, string|array $delete1 [, [$detele...])
    function filter_keys () {

        if (
    func_num_args() < 2) { return; }

        
    $args   func_get_args();
        
    $source array_shift($args);

        for (
    $i 0$l count($args); $i $l$i++) {

            
    $tmp $args[$i];

            if (
    is_array($tmp)) {
                foreach (
    $tmp as $k) {
                    unset(
    $source[$k]);
                }
            } else {
                unset(
    $source[$tmp]);
            }

        }

        return 
    $source;

    }

    $data = array('remove' => 'sometext''keep' => 'moretext''gone' => 225'stay' => 556'test' => 78787);

    print_r($data);

    $data filter_keys($data'remove''gone''test');

    print_r($data);
    Last edited by logic_earth; Aug 18, 2007 at 12:52. Reason: Error in Code
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •