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. :nono:

categoryfinder.php


<?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!

I’m allergic to regex :smiley:

<?php

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 ' ';

}

echo getCategory();

?>

Hi, Denny, and thanks for your reply.

Unfortunately, this has not gotten me the category returned.

Also, I am running this function inside a class, CategoryFinder. It is accessed from this code, which the category object is created about midway through.


<?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);
				
				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);
		}
	}
?>

Gracias,
Tyler

Tyler:

My code works, I just tested it again. You can put the function inside a class, no problem.

There can be many reasons why you are not getting a category:[LIST=1]
[]The test url does not have one of the keywords in it
[
]The getCategory() function never gets called (status <> 1)
[*]Something else
[/LIST]
To test it, add an echo statement to the getCategory() function to print out the url to check for keywords.

So, here is the solution. From the $_SERVER variable, I was getting Scripts/confirmform.php stored in the $currentURL variable. I switched the server variable to $_SERVER[‘HTTP_REFERRER’] & this solved the problem. It was not getting the current URL because I am using AJAX to initialize the PHP scripts from my jQuery/JS


<?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 ' ';
		}
	}
?>

Thank you very much for your help and support,

Tyler

My pleasure! :wink: