#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    55
    Rep Power
    12

    substr() condition and header


    Not very experienced in substr(), I tried checking the whole devshed site for an answer but either I didn't find one or I'm not understanding it.
    I thought the following would check for @my_domain.co.uk within $email and if it returned any value (meaning @my_domain.co.uk *was* present???) would make the header call the guest page again.

    PHP Code:
    <?
    if (substr($email"@my_domain.co.uk"))
    {
    header("location:guest.php");
    exit();
    }
    (Sorry, we all have to learn...)
  2. #2
  3. The Scott Spirit
    Devshed Novice (500 - 999 posts)

    Join Date
    Mar 2005
    Location
    Holland
    Posts
    569
    Rep Power
    36
    Try using [PHPNET="str_pos"]str_pos()[/PHPNET] instead of substr. Note the === in the if statement.

    PHP Code:
    <?
    if (strpos($email"@my_domain.co.uk") === true)
    {
    header("location:guest.php");
    exit();
    }
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    30
    Rep Power
    0
    I would use strstr() or strpos()
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2002
    Posts
    55
    Rep Power
    12
    I tried it and it was no go; then I tried it without the === true, i.e. just

    if (strpos($email, "@my_domain.co.uk"))
    {
    header("location:guest.php");
    exit();
    }

    and all seemed to go swimmingly.

    Thank you.
    iainlang@johnstone-wheelers.co.uk
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,868
    Rep Power
    368
    Originally Posted by iainlang
    I tried it and it was no go; then I tried it without the === true, i.e. just

    if (strpos($email, "@my_domain.co.uk"))
    {
    header("location:guest.php");
    exit();
    }

    and all seemed to go swimmingly.

    Thank you.
    you might also want to test this to see what happens if that @my_domain.co.uk is not your domain.. to make sure it still works
  10. #6
  11. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,660
    Rep Power
    4123
    strpos returns false or an integer, so the only value returned by strpos that will be matched by === is false, which === can't do. you would use !== instead

    if (strpos($email, "@my_domain.co.uk")) {...}

    only works for you because @my_domain.co.uk is not at the start.

    What if you did this:

    PHP Code:
    if(strpos("AB","A")) {
         echo 
    "A found";
    } else {
         echo 
    "A NOT found";

    You would only ever get "A NOT found" because the integer returned by strpos is 0 - "A" exists at the 0 indexed key, and PHP loosely evaluates 0 to false. The way round this is to do

    PHP Code:
    if(strpos("AB","A") > -1) {
         echo 
    "A found";
    } else {
         echo 
    "A NOT found";

    0 is greater than -1; false is not so A is found
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

IMN logo majestic logo threadwatch logo seochat tools logo