SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mysterious Error [8]

    I recently put in place a new error handler that e-mails me all of the errors that occur on my site (instead of just the select error handling I had in place before). I got most of the errors taken care of rather quickly (small ones like "index not found" in an array, or the use of date() without specifying a timezone), but I have one error (on a very common page on the site) that I just can't figure out! I don't know what I'm doing incorrectly, but it's driving me nuts

    Here's the error message:
    Unknown Error [8] Undefined index: Cust_Login Unknown error type on line 390 in file /home/httpd/vhosts/....
    Here's the line of code:
    Code:
    		<td><input type="text" size="40" name="Customer_Login" id="Customer_Login" value="<?php echo(!empty($_POST['Customer_Login'])) ? htmlspecialchars($_POST['Customer_Login']) : (!empty($_COOKIE['Cust_Login'])) ? htmlspecialchars($_COOKIE['Cust_Login']) : '' ?>" /></td>
    The error doesn't happen every time, but it somehow gets triggered 20+ times a day (when the page probably gets a couple hundred pageviews). Can anyone see what might be causing this? I think it's giving me a headache
    MySQL v5.1.58
    PHP v5.3.6

  2. #2
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When the Cust_Login cookie is not set and you try to access it, the error is raised.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  3. #3
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought empty() handles that correctly (it does for POST and every other var). Is there a better way to check if a cookie isset()?
    MySQL v5.1.58
    PHP v5.3.6

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrandonK View Post
    I thought empty() handles that correctly (it does for POST and every other var). Is there a better way to check if a cookie isset()?
    Why use empty()? Just use isset()
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  5. #5
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm always in the habit of using empty() because it also checks for an empty string/null value. I'm sure its better practice to use isset()... just another habit I need to break.
    MySQL v5.1.58
    PHP v5.3.6

  6. #6
    SitePoint Evangelist
    Join Date
    Jun 2004
    Location
    California
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BrandonK View Post
    I'm always in the habit of using empty() because it also checks for an empty string/null value. I'm sure its better practice to use isset()... just another habit I need to break.
    Its not a terrible habit. I actually think its fine but if notices are on in PHP, it will still error that you have an undefined index. But the code works fine doesn't it? (Despite the error, its not a fatal).

    So if you really don't mind you can just keep it in there and turn notices off. You won't have errors on in a production environment anyways.
    Happy switcher to OS X running on a MacBook Pro.

    Zend Certified Engineer

  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)
    Function combining, useless but why not!

    PHP Code:
    function sempty ($var) {
        return (!isset(
    $var) || empty($var)) ? true false;
    }

    if (
    sempty($var)) {} 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any idea why the following code resolves the error?:
    Code:
    <td><input type="text" size="40" name="Customer_Login" id="Customer_Login" value="<?php echo(!empty($_POST['Customer_Login'])) ? htmlspecialchars($_POST['Customer_Login']) : ((isset($_COOKIE['Cust_Login'])) ? htmlspecialchars($_COOKIE['Cust_Login']) : '') ?>" /></td>
    All I did was wrap the conditional in an additional set of parenthesis....
    MySQL v5.1.58
    PHP v5.3.6


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
  •