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

    Join Date
    May 2009
    Location
    Atlanta, GA, USA (unfortunately)
    Posts
    59
    Rep Power
    6

    Exclamation Category Finder Class Does Not Find a Category


    Hello all,

    I am interested to pass in a category that tells me what page the user was on when signing up for the mailing list, which will help me as an internet marketer to know what they may be interested in; however, the code I'm utilizing now is returning nothing but a blank string.

    categoryfinder.php
    PHP Code:
    <?php
        
    class CategoryFinder
        
    {
            public function 
    getCategory()
            {
                
    // Get the current page URL
                
    $currentURL $_SERVER['PHP_SELF'];
        
                
    // Build the regular expression and the match to array
                
    $matchTo = array(
                    
    'index'     => 'Home Page',
                    
    'health'    => 'Health',
                    
    'insurance' => 'Insurance',
                    
    'general'   => 'General',
                    
    'dating'    => 'Dating',
                    
    'education' => 'Education',
                    
    'legal'     => 'Legal',
                    
    'startyour' => 'Business Opportunities',
                    
    'business'  => 'Business Services'
                
    );
        
                
    $regExp  '/' join('|'array_keys($matchTo)) . '/i';
        
                
    // Attempt to match something from the URL
                
    if (preg_match($regExp$currentURL$matches)) {
                    return 
    $matchTo[$matches[0]];
                }
        
                return 
    ' ';
            }
        }  
    ?>
    I really appreciate the help!
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    Well, the case-insensitivity of your regex can cause problems: it might find "INDEX" but that's not a key in the $matchTo array.
    Otherwise what's the value of $currentURL?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Atlanta, GA, USA (unfortunately)
    Posts
    59
    Rep Power
    6

    Unhappy


    I would guess that the value of $currentURL is going to be http://www.worldreviewgroup.com/index.html, which is the URL I'm testing this script on, which ideally, should return "Home Page" as the category.

    I don't understand how my regEx is case-insensitive...
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,128
    Rep Power
    9398
    Rather than guessing, how about outputting it and seeing what it really is?

    PHP Code:
    $regExp  '/' join('|'array_keys($matchTo)) . '/i'
    The /i makes it case-insensitive.

    Comments on this post

    • etidd agrees : Not the solution yet, but thanks. You have lots of knowledge.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Atlanta, GA, USA (unfortunately)
    Posts
    59
    Rep Power
    6

    Angry


    Hi, requinix, I re-wrote the class function without a regex, which still returns nothing when called by the confirmform.php file. I tried to echo the variable, but I'm not seeing anything still.

    PHP Code:
    <?php
        
    class CategoryFinder
        
    {
            public function 
    getCategory() {
                
    $currentURL $_SERVER['PHP_SELF']; 
                     
                
    // Build the match to array 
                
    $matchTo = array( 
                    
    'index'     => 'Home Page'
                    
    'health'    => 'Health'
                    
    'insurance' => 'Insurance'
                    
    'general'   => 'General'
                    
    'dating'    => 'Dating'
                    
    'education' => 'Education'
                    
    'legal'     => 'Legal'
                    
    'startyour' => 'Business Opportunities'
                    
    'business'  => 'Business Services' 
                
    ); 
                
                foreach(
    $matchTo AS $key => $value) {
                    if(
    strpos($currentURL$key) !== FALSE) {
                        return 
    $value;
                        break;
                    }
                }
                
                return 
    ' ';
            
            }
        }
    ?>
    the confirmform.php file creates an object and runs it (to no avail). That segment is about halfway down.
    PHP Code:
    <?php
        $instance 
    = new CheckForm;
        
    $instance -> checkSubmission();
        
        class 
    CheckForm
        
    {
            public function 
    checkSubmission()
            {    
                
    $origEmail $_POST['origEmail'];
                
    $confirmEmail htmlspecialchars($_POST['confirmEmail']);
                
    $name htmlspecialchars($_POST['name']);
                
    $ageRange $_POST['age'];
                
    $gender $_POST['gender'];
                
    $country $_POST['country'];
                
    $catcher htmlspecialchars($_POST['catcher']);
                
    $mathAnswer htmlspecialchars($_POST['addition']);
                
    $rightAnswer $_POST['mathAnswer'];
                
    $submissionTime $_POST['submissionTime'];
                
    $status 0;
            
                
    $response = array("validation" => " ""message" => " ""database" => " ");
                
                if (empty(
    $confirmEmail) && empty($name) && $country === "Select Country") {
                    
    $response['message'] = "That's not a valid submission.";
                } elseif (empty(
    $confirmEmail) && $country === "Select Country"){
                    
    $response['message'] = "Please confirm your e-mail and select a location.";
                } elseif (empty(
    $name) && $country === "Select Country"){
                    
    $response['message'] = "Please enter a name and select a location.";
                } elseif (empty(
    $name)) {
                    
    $response['message'] = "Please enter a name.";
                } elseif (empty(
    $confirmEmail)) {
                    
    $response['message'] = "No confirmation e-mail was entered.";
                } elseif (
    $origEmail != $confirmEmail) {
                    
    $response['message'] = "E-mail addresses don't match.";
                } elseif (
    $country === "Select Country") { 
                    
    $response['message'] = "Please select a location.";
                } elseif (
    $mathAnswer != $rightAnswer) {
                    
    $response['message'] = "Math answer is incorrect.";
                } elseif (!empty(
    $catcher)) {
                    
    $response['message'] = "Bot submission.";
                } elseif (
    $submissionTime <= 8000) {
                    
    $response['message'] = "Woah! Slow down and fill out the form.";
                } else
                    
    $status 1;
                    
                if (
    $gender === "Male")
                    
    $gender "M";
                elseif (
    $gender === "Female")
                    
    $gender "F";
                else
                    
    $gender NULL;

                
                if (
    $status === 1) {
                    require_once(
    "categoryfinder.php");
                    
    $categoryFinder = new CategoryFinder;
                    
    $category $categoryFinder -> getCategory();
                    echo 
    '   category:  ' $category;
                    
                    
    $response['validation'] = "pass";
                    
    $response['message'] = "Thanks for joining the e-mail list, <b>" $name "</b>, under the e-mail address, <b>" $confirmEmail "</b>.";
                            
                    require_once(
    'databasewriter.php');
                    
    $dbWriter = new DatabaseWriter;
                    
    $dbCode $dbWriter -> writeUserToDatabase($confirmEmail$name$ageRange$gender$country$category);
                    
                    echo 
    '    database code: ' $dbCode;
                    if (
    $dbCode === 1) {
                        
    $response['database'] = "pass";
                        echo 
    'Database Write Successful';
                    } else {
                        
    $response['database'] = "fail";
                        
    $response['validation'] = "fail";
                        echo 
    'Database Write Failure';
                    }
                    if (
    $dbCode == 2) {
                        
    $response['message'] = "Server error. Please try again later.";
                    } elseif (
    $dbCode == 3) {
                        
    $response['message'] = "That e-mail address already exists.";
                    }
                }
            
                echo 
    json_encode($response);
            }
        }
    ?>
    Thanks again for your help. Here's some rep points.
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    does the getCategory method even run? Are you sure you're sending the text/html content type rather than application/json? In other words: If you put an echo on top of your method, do you see the output? If yes, do a var_dump() of $currentURL and post the result here. If not, the problem is elsewere, it happens before you category code even runs.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Atlanta, GA, USA (unfortunately)
    Posts
    59
    Rep Power
    6

    Arrow


    Great post, Jacques1. Here is what I found out with a var_dump() of $currentURL, after echoing inside of the getCategory() function revealed that, yes, the function is running.

    Current URL
    string(24) "/Scripts/confirmform.php"

    Obviously, confirmform.php will not match any of the indices of the array. So, where to go from here?
  14. #8
  15. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    I'm a bit confused. How does a visitor get from, say, index.html to your PHP script? Do you make an AJAX call?

    Because obviously you execute the script in a separate request. Either you use AJAX, or you submit the form to that PHP file. In any case, the request URL will not include the category. If it's not in the referrer, you'll have to explicitly send it.
    Last edited by Jacques1; March 31st, 2013 at 07:55 PM.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Atlanta, GA, USA (unfortunately)
    Posts
    59
    Rep Power
    6
    I am using AJAX in order to send data from JavaScript into the PHP script.

    I guess I can make a JavaScript variable and send it along with the rest of the form data? Maybe there's a better way that you recommend to explicitly send the URL of the current HTML document.
  18. #10
  19. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    The URL should be in the referrer: $_SERVER['HTTP_REFERER'].
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Location
    Atlanta, GA, USA (unfortunately)
    Posts
    59
    Rep Power
    6

    Thumbs up Thank You


    I changed the script in a very minor way. This is the solution.

    PHP Code:
    <?php
        
    class CategoryFinder
        
    {
            public function 
    getCategory() {
                
    $currentURL $_SERVER['HTTP_REFERER']; 
                
    var_dump($currentURL);
                
                
    // Build the match to array 
                
    $matchTo = array( 
                    
    'worldreviewgroup'     => 'Home Page'
                    
    'health'    => 'Health'
                    
    'insurance' => 'Insurance'
                    
    'general'   => 'General'
                    
    'dating'    => 'Dating'
                    
    'education' => 'Education'
                    
    'legal'     => 'Legal'
                    
    'startyour' => 'Business Opportunities'
                    
    'business'  => 'Business Services' 
                
    ); 
                
                foreach(
    $matchTo AS $key => $value) {
                    if(
    strpos($currentURL$key) !== FALSE) {
                        return 
    $value;
                        break;
                    }
                }
                
                return 
    ' ';
            
            }
        }
    ?>
    -Tyler

IMN logo majestic logo threadwatch logo seochat tools logo