Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts

    something is wrong :(

    hey all,, hope u are havin a good day,,

    there is something bad about this code,,, i edited it to add a third selection but it was lol

    PHP Code:
    <?php 

    if (isset($_POST['style']) && $_POST['style'] == 'blue'

    $style_sheet 'blue'
    } else { 
    $_POST['style'] == 'default')
    {
    $style_sheet 'default'
    }} else { 
    $_POST['style'] == 'green')
    {
    $style_sheet 'green'
    }}
    setcookie('css',$style_sheet,time()+999999999); 

    ?>

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    Scotland
    Posts
    407
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try simplifying things a bit:

    PHP Code:
    <?
    if (isset($_POST['style'])) 
    {
      
    $style_sheet $_POST['style']; 
    } else { 
      
    $style_sheet "default"
    }

    setcookie('css',$style_sheet,time()+999999999); 

    ?>
    My body's a temple... and like those ancient Greek ones it's a ruin
    sweenster.co.uk

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    perfect!

    thanks alot bro

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    If I were you, I'd slightly modify that code to only allow what I say should go through. I suppose I'm a little paranoid when it comes to this, but I wouldn't ever trust a user input as being valid:
    PHP Code:
    $allowed_stylesheets = array(
         
    'blue',
         
    'green'
    );

    if (isset(
    $_POST['style']) AND in_array($_POST['style'], $allowed_stylesheets))
    {
         
    $style_sheet $_POST['style'];
    }
    else
    {
         
    $style_sheet 'default';
    }

    setcookie('css',$style_sheet,time()+999999999); 

  • #5
    Regular Coder
    Join Date
    Nov 2004
    Location
    The Netherlands
    Posts
    551
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Why not use a switch to do the job?
    CATdude about IE6: "All your box-model are belong to us"

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Arrays are easyer to maintain... the best [possibility would be to check if a stylesheet exists and including it. Installing a new stylesheet would be limited to uploading the file to a folder.
    Quote Originally Posted by Fou-Lu
    If I were you, I'd slightly modify that code to only allow what I say should go through. I suppose I'm a little paranoid when it comes to this, but I wouldn't ever trust a user input as being valid
    If paranoid is common sense...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by marek_mar
    Arrays are easyer to maintain... the best [possibility would be to check if a stylesheet exists and including it. Installing a new stylesheet would be limited to uploading the file to a folder.

    If paranoid is common sense...
    lol
    I picked that up from a combination of yours and rafs responses. Prior to my posting here, I hadn't realized how poorly coded my work had been. That reminds me, I haven't seen raf in like forever.
    Anyway.
    You could use a switch for this if you really wanted to, but I don't see much by way of expansion for that with the validation. You would need to create a case for each individual style that you would like to use, which would be alright but suppose you wanted to start doing like ten themes. The ten lines of code that I've posted there will be a lot easier to maintain than adding a new case for each one, smaller in size as well. And practically speaking, I'd say that the load time of taking an element from an array and comparing it to the existance of another value is probably quicker than that of using a switch and case feature, considering that all values would be comming from the same place. Honestly though, this would mearly be a matter of taste I suppose, the time difference for whichever would end up quicker would be so minute, you couldn't record it on anything less than a thousandth of a second. So its a matter of choice.

  • #8
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    By "maintain" I ment everything you wrote after "anyway" But you used the word "maintain" so it can't be a definition.
    It's always good to check input data as sometimes unexpected things may hapen. Some very serious phpBB bug was caused by not checking a varaible type.
    BTW Raf was here a few days ago.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #9
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu
    That reminds me, I haven't seen raf in like forever.
    i know... to much work...

    i personally prefer to use arrays because you can create the array in a seperate file, and then include it where you need it. in some pages, you'll include it and check if there is a member with that arrayname (like in this situation) and in another page, you'll use the array to build a listbox where the user can select a stylesheet etc.
    + arrays are for me a datasource, where a switch is a language-contruct to implement some logic.
    in this situation, you just wanna know if the stylesheetname is a member of your stylesheet-collection, so i don't realy see why you'd use a switch.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #10
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    You need to read our posting guidelines specifically number 2.

    http://www.codingforums.com/postguide.htm
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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