SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast Fdb's Avatar
    Join Date
    Dec 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question $genre must be one of these?

    I have looked everywhere and read all my books but cannot get it right.

    I have a variable, $genre, and $genre must be one of these: pop, rock, metal, jazz, latin, .... (There are 52 genres)

    and if it is not of those genres, it must be pop

    I have tried to put the pop, rock etc. into an array and use it with an if satement, but nothing works.

    My url ends with index.php?genre=pop for example.

    If someone goes now and edit the url to index.php?genre=blahblah

    the script must see that blahblah is not one of the genres supported and use pop instead.

  2. #2
    SitePoint Wizard holmescreek's Avatar
    Join Date
    Mar 2001
    Location
    Northwest Florida
    Posts
    1,707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This would be a good case to store the genre's into a MySQL database. Do a simpl select * from mytable where $genre=$requested_genre

    If the query returns false, then set the $requested_genre to default to "Pop" then process your page output based on $requested_genre -- or if the query returns false you could display an error page.

    Oh, be sure to first strip any html or code out of the $requested_genre variable that is passed by the user first to avoid a sql injection.
    intragenesis, llc professional web & graphic design

  3. #3
    SitePoint Enthusiast Fdb's Avatar
    Join Date
    Dec 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have this script at the top of my page:

    <?php

    $cachedir = '1/cache/';
    $cachetime = 1209600;
    $cacheext = 'cache';
    $ignore_list = array(
    'help.html',
    'contact_us.html'
    );
    $page = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $cachefile = $cachedir . md5($page) . '.' . $cacheext;

    $ignore_page = false;
    for ($i = 0; $i < count($ignore_list); $i++) {
    $ignore_page = (strpos($page, $ignore_list[$i]) !== false) ? true : $ignore_page;
    }

    $cachefile_created = ((@file_exists($cachefile)) and ($ignore_page === false)) ? @filemtime($cachefile) : 0;
    @clearstatcache();
    if (time() - $cachetime < $cachefile_created) {
    @readfile($cachefile);
    exit();

    }
    ob_start();

    ?>

    It caches the pages, because if my website gets too busy, it gets slow because of all the MySQL connections etc. The pages change only about once every 2 weeks, that is why the $cachetime is 1209600 seconds.

    The above script is the very first part, because it checks if the page is cached, and if not, cache it.

    Some people now change the url from index.php?genre=pop to index.php?genre=1, index.php?genre=2..... index.php?genre=99999 causing 99999 pages to be cached that state "Genre xxxx is not currently supported. Select another genre...." !!

    That is why that just after the <?php above I want to put in something that $genre can only be one of 52 types.

  4. #4
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and you can use a
    if (isset ($_GET['genre'])) {
    your code
    }
    else {
    $genre = 'pop';
    }

    BTW way this is not working code

    and me personally, I like to turn everthing into numbers, so if you decide to use the SQL option, you can use the primary key and hide the name from the url using $_post instead - I just like to keep everything i can out of the browser's address bar

    cool
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  5. #5
    SitePoint Enthusiast Fdb's Avatar
    Join Date
    Dec 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have put this right after the <?php

    $genrecheck = array('pop','rock','metal','jazz');
    if (in_array($genre, $genrecheck)) {
    $genre = $genre;
    } else {
    $genre = 'pop';
    }

    I have tried it and if I now change the url to index.php?genre=blahblah

    it just show the cached Pop page, and do not cache a blahblah page.

  6. #6
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    should it not be
    $genre = $_GET['genre']; - so it is referenceing your get var?
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  7. #7
    SitePoint Enthusiast Fdb's Avatar
    Join Date
    Dec 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. You are right. Thanks.

  8. #8
    SitePoint Enthusiast Fdb's Avatar
    Join Date
    Dec 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh...

    I changed the $cachetime to 20 seconds, and it is still caching a blahblah page.

  9. #9
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // remember to always filter your input to prevent injection
    $genre preg_replace('/[^a-z]/'''$_GET['genre']); // delete all but lower case
    $genrecheck = array('pop','rock','metal','jazz'); 
    if (! 
    in_array($genre$genrecheck)) { 
        
    $genre 'pop';

    Christopher

  10. #10
    SitePoint Guru MikeBigg's Avatar
    Join Date
    Jun 2004
    Location
    Reading, UK
    Posts
    970
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kirikintha
    and me personally, I like to turn everthing into numbers, so if you decide to use the SQL option, you can use the primary key and hide the name from the url using $_post instead - I just like to keep everything i can out of the browser's address bar
    So you would prefer something like index.php?g=17 rather than index.php?genre=jazz ?

    Interesting SEO strategy

    Mike

  11. #11
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm so used to primary keys in my DB that I reference everything by number (or encrypted prefix!) - I have to use a lot of sensitive info, so if there is anything I can do to keep other people out of my business that's what I do - plus, I've had some awful issues with strings, and ID-ing everything as number allows me to use comma delimited strings to do multiple updates and deletes without having to worry about something going wrong with a string.

    It's just solved a lot of problems for me, plus I can reference multiple tables as well

    GO REFERENTIAL INTEGRITY! and thanks to my DB teacher he was and is the best!
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"

  12. #12
    SitePoint Enthusiast Fdb's Avatar
    Join Date
    Dec 2005
    Posts
    93
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I changed the code for the cache part, and tested it this time with a $cachetime of 20 seconds a few times, and no extra cache files were created if I changed the url to index.php?genre=9898 or index.php?genre=blahbah etc.

    PHP Code:
    <?php
    $genre 
    preg_replace('/[^0-9]/'''$_GET['genre']);
    $genrecheck = array('pop','rock','metal','jazz'); 
    if (! 
    in_array($genre$genrecheck)) { 
        
    $genre 'pop'

    $cachefile "1/cache/".md5($genre).".cache";
    $cachetime 20;
          
          if (
    file_exists($cachefile) && (time() - $cachetime
             
    filemtime($cachefile))) 
          {
             include(
    $cachefile);
             
             exit;
          }
    ob_start(); ?>

  13. #13
    SitePoint Addict kirikintha's Avatar
    Join Date
    Nov 2004
    Location
    Boulder, CO
    Posts
    290
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh BTW arbor your code rocks as usual! it makes me think of things so differently!
    myspace.com/tarsus
    Technical Coordinator
    BCMT
    "Not impossible, just hard to do!"


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
  •