Case sensitive , add strtolower [duplicate] - php

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Users to register only lower case letters
Hi, I have this register page , and i would like to have the script register only lower case letters : pretty
I do not want it to register :Pretty , PRETTy , PRETTY ... Here is the code , what do i need to add to it ?
public function addField($field_name){
if (!array_key_exists($field_name, $this->fields))
{
if ($field_name=='username') {
$field = new field_join_username();
parent::registerField($field);
}
if ($field_name=='email') {
$field = new field_join_email();
parent::registerField($field);
}
}
parent::addField($field_name);
}

Where's that code taken from? Are you trying to change existing code?
As far as I can see, this only adds fields not values of fields where the strtolower should be applied, so applying strtolower to field_name won't do the trick ... but it's hard to guess where to place strtolower without seeing the correct code snippet.

Related

why is my email format checker not working? [duplicate]

This question already has answers here:
How to validate an email address in PHP
(15 answers)
Closed 2 years ago.
I have a PHP script that runs when a form button is clicked. Everything is working fine, apart from a routine that checks for email format. I have tried using the inbuilt PHP filter function for that but it doesnt even seem to run (I am using a suitable version of PHP for this). I am checking for the existence of the 'at' symbol and a dot for the domain name, just on the dev machine WAMP webserver at the moment. If I enter an invalid address (say abc123 - i.e., no 'at' symbol, no dot) it seems to think everything is OK and loads the appropriate page. Code here: (tempvar echoes correctly by the way, and is just there for experiment)
$_SESSION['emailaddress']=$_POST['unamebox'];
$tempvar = $_SESSION['emailaddress'];
function checkEmail($tempvar) {
$find1 = strpos($tempvar, '#');
$find2 = strpos($tempvar, '.');
return ($find1 !== false && $find2 !== false && $find2 > $find1);
}
if ( checkEmail($tempvar) )
{
echo "OK";
}
else
{
echo "Bad email format!";
}
Because strpos return an integer if found the template in string otherwise return false. Now the !== operator is also match variable types!
When find1 or find2 strpos is match, the !== return false value!

How can I str_replace partially in PHP in a dynamic string with unknown key content

Working in WordPress (PHP). I want to set strings to the database like below. The string is translatable, so it could be in any language keeping the template codes. For the possible variations, I presented 4 strings here:
<?php
$string = '%%AUTHOR%% changed status to %%STATUS_new%%';
$string = '%%AUTHOR%% changed status to %%STATUS_oldie%%';
$string = '%%AUTHOR%% changed priority to %%PRIORITY_high%%';
$string = '%%AUTHOR%% changed priority to %%PRIORITY_low%%';
To make the string human-readable, for the %%AUTHOR%% part I can change the string like below:
<?php
$username = 'Illigil Liosous'; // could be any unicode string
$content = str_replace('%%AUTHOR%%', $username, $string);
But for status and priority, I have different substrings of different lengths.
Question is:
How can I make those dynamic substring be replaced on-the-fly so that they could be human-readable like:
Illigil Liosous changed status to Newendotobulous;
Illigil Liosous changed status to Oldisticabulous;
Illigil Liosous changed priority to Highlistacolisticosso;
Illigil Liosous changed priority to Lowisdulousiannosso;
Those unsoundable words are to let you understand the nature of a translatable string, that could be anything other than known words.
I think I can proceed with something like below:
<?php
if( strpos($_content, '%%STATUS_') !== false ) {
// proceed to push the translatable status string
}
if( strpos($_content, '%%PRIORITY_') !== false ) {
// proceed to push the translatable priority string
}
But how can I fill inside those conditionals efficiently?
Edit
I might not fully am clear with my question, hence updating the query. The issue is not related to array str_replace.
The issue is, the $string that I need to detect is not predefined. It would come like below:
if($status_changed) :
$string = "%%AUTHOR%% changed status to %%STATUS_{$status}%%";
else if($priority_changed) :
$string = "%%AUTHOR%% changed priority to %%PRIORITY_{$priority}%%";
endif;
Where they will be filled dynamically with values in the $status and $priority.
So when it comes to str_replace() I will actually use functions to get their appropriate labels:
<?php
function human_readable($codified_string, $user_id) {
if( strpos($_content, '%%STATUS_') !== false ) {
// need a way to get the $status extracted from the $codified_string
// $_got_status = ???? // I don't know how.
get_status_label($_got_status);
// the status label replacement would take place here, I don't know how.
}
if( strpos($_content, '%%PRIORITY_') !== false ) {
// need a way to get the $priority extracted from the $codified_string
// $_got_priority = ???? // I don't know how.
get_priority_label($_got_priority);
// the priority label replacement would take place here, I don't know how.
}
// Author name replacement takes place now
$username = get_the_username($user_id);
$human_readable_string = str_replace('%%AUTHOR%%', $username, $codified_string);
return $human_readable_string;
}
The function has some missing points where I currently am stuck. :(
Can you guide me a way out?
It sounds like you need to use RegEx for this solution.
You can use the following code snippet to get the effect you want to achieve:
preg_match('/%%PRIORITY_(.*?)%%/', $_content, $matches);
if (count($matches) > 0) {
$human_readable_string = str_replace("%%PRIORITY_{$matches[0]}%%", $replace, $codified_string);
}
Of course, the above code needs to be changed for STATUS and any other replacements that you require.
Explaining the RegEx code in short it:
/
The starting of any regular expression.
%%PRIORITY_
Is a literal match of those characters.
(
The opening of the match. This is going to be stored in the third parameter of the preg_match.
.
This matches any character that isn't a new line.
*?
This matches between 0 and infinite of the preceding character - in this case anything. The ? is a lazy match since the %% character will be matched by the ..
Check out the RegEx in action: https://regex101.com/r/qztLue/1

CakePhp, how MySQL "Like" works as case insensitive search [duplicate]

This question already has answers here:
How can I search (case-insensitive) in a column using LIKE wildcard?
(16 answers)
Closed 8 years ago.
In CakePhp, how MySQL Like works as case insensitive search.
I have tried with following code but problem is also coming here.
If i am searching "Motel Park" then result is fine.
But when searching "motel park" no result is found.
In database Collation is latin1_swedish_ci
$where_condition = array('Ad.completed' => 1,
'Ad.address LIKE '=>'%'.$_REQUEST['address'].'%',
'Ad.status' =>$ad_status
);
$result = $this->Ad->find('all',array('conditions'=>$where_condition));
You can convert both the value to lowercase to use LIKE statement
$where_condition = array(
'Ad.completed' => 1,
'LOWER(Ad.address) LIKE '=>'%'.strtolower($this->request->data['address']).'%',
'Ad.status' =>$ad_status
);
$result = $this->Ad->find('all',array('conditions'=>$where_condition));
try using "COLLATE utf_general_ci" collation. refer this link.
but there is an easy solution , by converting all string in to lowercase. then check.
$request_address=strtolower($_REQUEST['address']);
$where_condition = "Ad.completed = 1 AND
LOWER(Ad.address) LIKE '%".$request_address."%' AND
Ad.status ='".$ad_status."'";
$result = $this->Ad->find('all',array('conditions'=>$where_condition));

php return types or enums? [duplicate]

This question already has answers here:
Enumerations on PHP
(39 answers)
Closed 1 year ago.
I am using Codeignitor for programming and coming from c#.
its look like a newbie question ,but i want to do something like this.
I have some functions that which return some values :
Example :
function Create_db()
{
//do something
if (PassWordError) //some errors here it is password incorrect
{
return "PASS_ERROR";
}
}
and in some places i am using like this
$result = Create_db(); //assume that there are some problems and returned "PASS_ERROR"
so that the $result is string now,
is there anything like ENUMS in php ? or something like that ?
so that i can do something like this :
if (passWordError)
{
return $error->PasswordError();
}
i assume that i can have some others types too
like
Maximum_name_error,authenticationFailedError etc
Thank you.
A typical approach to doing this in PHP would be to use class constants:
class Something {
const ERR_HI = 'hi';
const ERR_FOOBAR = 'foobar';
}
//to use:
return Something::ERR_FOOBAR;
Although I would consider using exceptions instead.
A good way to deal with this in CI is to use the Language Class - http://codeigniter.com/user_guide/libraries/language.html
Lang file:
$lang = array (
'password_error' => 'The password you entered is incorrect.',
....
)
Controller:
if ($PassWordError) //some errors here it is password incorrect
{
return $this->lang->line('password_error');
}
IMO, when something went wrong, you should throw a custom (or not) exeption and manage it with an exeption handler in your application.
if (passWordError)
{
throw new BadPasswordExeption($params, $you, $need);
}
Then you can surround it with a try catch block.
You can find more informations here: http://php.net/manual/en/language.exceptions.extending.php

Is this enough for a secure site? (4 small functions) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP: the ultimate clean/secure function
I revised my site's security filters today. I used to filter input and do nothing with the output.
Here it is:
All user inputted variables go through these 2 functions depending on the type:
PS: Since I didn't start coding from scratch I did it for all variables, including the ones that aren't aren't used in queries. I understand that this is a performance killer and will be undoing that. Better safe than sorry right?
// numbers (I expect very large numbers)
function intfix($i)
{
$i = preg_replace('/[^\d]/', '', $i);
if (!strlen($i))
$i = 0;
return $i;
}
// escape non-numbers
function textfix($value) {
$value = mysql_real_escape_string($value);
return $value;
}
XSS preventing:
Input - filters user submitted text, like posts and messages. As you see it's currently empty. Not sure if strip_tags is needed.
Output - on all html outputs
function input($input){
//$input = strip_tags($input, "");
return $input;
}
function output($bbcode){
$bbcode = textWrap($bbcode); // textwrap breaks long words
$bbcode = htmlentities($bbcode,ENT_QUOTES,"UTF-8");
$bbcode = str_replace("\n", "<br />", $bbcode);
// then some bbcode (removed) and the img tag
$urlmatch = "([a-zA-Z]+[:\/\/]+[A-Za-z0-9\-_]+\\.+[A-Za-z0-9\.\/%&=\?\-_]+)";
$match["img"] = "/\[img\]".$urlmatch."\[\/img\]/is";
$replace["img"] = "<center><img src=\"$1\" class=\"max\" /></center>";
return $bbcode;
}
I included the img tag because it could be vulnerable to css...
What do you think? Anything obviously wrong? Good enough?
Looks ok, but you could easily make one function for both texts and ints, checking first its type, and act on it.

Categories