Jump to content

Recommended Posts

how do i check to see whether a string only has certain variables. I'm getting the category variable through GET. The only categories there are are sports, performance, organization, random, and surrounding. so i want to check to see if the words match. something like below

 

<?php

$category = urldecode($_GET['category']);

//check to see if it ONLY consists of the categories above

Link to comment
https://forums.phpfreaks.com/topic/172121-only-certain-values-in-string/
Share on other sites

thats what i had, but it's not working...

 

<?php
//set category
$accepted_values = array('organization', 'sports', 'performance', 'random', 'surrounding');
$cate= urldecode($category);
if(in_array($cate, $accepted_values))
    {
     $cat = " AND event IN $cat_sql ";
    }else 
    { 
     $cat = "";
    }

 

my $cat variable is empty when i do something like this

 

mysite.com/api.php?category=sports+performance

 

it works when i just use one category in the url though

Category is in the GET array. It is not global!

<?php
if(in_array(urldecode($_GET['category']), array('sports', 'performance', 'organization', 'random', 'surrounding'))) {
print "valid";
}
else {
print "invalid";
}
?>

mysite.com/api.php?category=sports+performance would return 'sports performance'. This is not a category! You should not pass category names through the url in this fashion especially as strings! Use their database ids

 

thats a negative on that one too...same thing, works with one category but not with two

What do you mean "with two". Is $_GET['category'] a list of category values rather than a single value?

 

Where does $cat_sql come from? what value does it contain?

$cat = " AND event IN $cat_sql ";

 

thats a negative on that one too...same thing, works with one category but not with two

Because you would need to explode the values in the url as an array. If you use strings you have no delimeter. However ids could be comma separated

i.e. mysite.com/api.php?category=1,2,75

<?php
$categories = explode(",",$_GET['category']);
$validUrlCategories = array();
foreach($categories as $category) {
if(in_array($category, array(1,2,3,4,75))) {	
	$validUrlCategories[] = $category;
}
}
if(count($validUrlCategories)) {
print "the url contains the following valid categories: ".implode(",", $validUrlCategories);
}
else {
print "no valid categories in url";
}
?>

yeah thats my whole goal, to have multiple values for $_GET['category']. So

 

mysite.com/api.php?category=performance

mysite.com/api.phP?category=performance+sports

mysite.com/api.phP?category=performance+sports+random

 

should all  be acceptable

 

mysite.com/api.phP?category=performance+sports+23dsgkl

 

should not be accepted

 

$cat_sql is formatting the words found in $_GET['category'] with apostrophes and commas to be right for the query. It works fine.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.