Is this a PHP 'bug' or am I missing something? - php

Does anyone know anything about this problem, I have the following code:
if (strtotime($unlockInfo->UnlockReviewDate) < time()) {
echo "<h3>Please review your details and ensure they are accurate and up to date.</h3>";
$verifState = validateUnlockCode($db_conn, $unlockInfo->UnlockCode);
}
Now as is it seems to work fine but there was a problem that I seemed to be chasing around for ages.
Essentially $unlockInfo is an object returned from a mySql query which as you can probably see is evaluated against the current time. Now the validateUnlockCode function has the ability under specific circumstances to modify the database and therefore the $unlockInfo object.
Nothing, however, should be modified until after the if statement is evaluated. but when I miss the space out from the if statement, ie.
if(strto....
this seems to cause the $verifState to be set before the if is evaluated therefore calling the validate function and modifying the database premeturely.
Is this normal? is that supposed to happen? Sorry, I'm a bit confused on this one.

The space after the if does not affect anything.
If strtotime($unlockInfo->UnlockReviewDate) fails, then it (false == 0) will surely be less than the current UNIX timestamp. That'd be the first thing I check.
What does strtotime return? What is the format of UnlockReviewDate?

Related

Is it OK to design code that throws basic warnings, or should I change my code?

with warning, I mean warnings that are not important or breaking, and not wrong code per say.
for example, imagine I have a search page.
$query, $items (as in how many items to display)
and I would still supply the page with only $query, and I would use some default value for $items.
Now, since my code is basically
$query = $_GET["query"];
$items = $_GET["items"];
after this I can write such code to show items,
if (empty($items))
//show default number of items
else
//show set amount of items
Of course I would need to check if items is not number and stuff like that, but the basic idea is this.
This obviously throws warning "items" is not set. To fix this, before doing $items, I can check if "items" exists, and maybe if it exists set it to that amount and if not the default amount. This would throw nothing. However this only makes my code either longer or just the same logic at different place. I would still need to check if $items is number and tons of other stuff anyway, its not only one check either, although it is still short in this case. At best case, I would end with basically same thing in different place.
So what should I do? Make php ignore warnings for error_log ? Or should I make sure this errors never happen even if it doesnt change how the code works and make my "uglier" ?
Are warnings, warnings or just suggestions?
After some research and a bit of thinking, I have written sensible variations of the code above. the solutions always, naturally, include using $_GET or ignoring errors/warnings/notices. this is not ideal.
I thought to see if filter_var would be useful for this, it is not.
I have created several solutions and the most feasible one seems to be having a function for $_GET
example:
function setVar($v){
if (isset($_GET[$v]))
return $_GET[$v];
}
As expected, this will be just a reference to the original variable so nothing is changed. Except it wont throw notices. (isset and such will still work for unset/empty parameters as I dont return anything if it fails anyway) and GET/POST etc. will work in all scopes so I can use this in a "functions.php".
Also on a side note, for php 5.3 and below, it seems you can use(while 5.4 & above causes error)
function x($_GET...
while it is not exactly traditional, I dont think it would be much different to use setVar('var') rather than $_GET['var']

Stored value from soap __getLastRequest() echos but won't register in array

I've got a problem that seems particularly odd (to me at least). So I'm using a soap API to interact with a program that (among other things) creates user groups and assigns users to them. I need to create a group and then assign a user to it using two different soap functions; this requires me to store the returned variable from one soap api call and input it into the next (see below).
$value = $SOAP->addOrganisation($params);
$group=$SOAP->__getLastResponse();
echo "<br/>Group: ".$group."<br/>";
$params=array('SID'=>$sid, 'user_id'=>$user, 'organisation_id'=>$group, 'insertedby'=>1);
$value= $SOAP->addUserToOrganisation($params);
Straight-forward right?
But here's where it gets interesting: the variable $group echos what I'm expecting but seems not to work in the soap call addUserToOrganisation. The returned value says that it ran fine, however addUserToOrganisation doesn't end up carrying out its task (actually adding the user to the group).
Conjectures anticipated: (Stuff I've already guessed at)
Maybe it's a glitch in the function addUserToOrganisation?
That would make sense and I thought the same. However when I hard-coded a number in there (even when it was the exact same number the variable would have echoed), it works just fine.
Maybe the API doesn't play nicely with variables in that spot in the array?
I actually reset the variable manually with a number (see below) and it worked no problem.
$value = $SOAP->addOrganisation($params);
$group=$SOAP->__getLastResponse();
echo "<br/>Group: ".$group."<br/>";
$group=55;
echo "<br/>Group: ".$group."<br/>";
$params=array('SID'=>$sid, 'user_id'=>$user, 'organisation_id'=>$group, 'insertedby'=>1);
$value= $SOAP->addUserToOrganisation($params);
Any stray spaces attached to that variable?
None that I can see.
...Maybe you pissed off the gnomes inside your computer that make it work?
Quite possibly but they've put up with my abuse for so long, why the sudden uprising?
My best guess: As far as I can tell, there just seems that there is something inherently unacceptable about using the output of __getLastRequest(), even though it just looks like a regular old number when echoed. I don't know if not all strings are equal or something but if any of you have an idea, I'd really appreciate it.
(just watch, it turns out to somehow be some dumb syntax error)
Anyway, thanks in advance

Replacing goto with a function, why doesn't this work?

I've been trying to change some code for a validation script so that it doesn't include goto, as I want to run my scripts on a webhost, and unfortunately most of them don't really have 5.3 or later.
The code is meant to use a value generated by rand for a unique validation number. If it finds it already in the DB, it will append 1 on to the number as many times as it needs to until it finds a usable number. Pretty straightforward. I know it's terrible practice for a MySQL query, but it's really just a simple script for practice.
The code is as follows:
function gen_val($val)
{
$query = mysql_query("SELECT val_id FROM users_tmp WHERE val_id=$val");
$row = mysql_fetch_array($query);
if ($row[0]==$val) {
gen_val($val+1);
} else {
return $val;
}
}
$val_x=gen_val(rand());
This returns a value '0' if the value already existed in the DB. Function works fine if there's no similar value already, so I think there's a problem with the function recursing. I don't think I really know how to use it properly, so please go ahead and educate me.
Cheers.
Asher
You forgot to return the result of your recursion.
return gen_val($val+1);
Correct me if I'm wrong, but you want to get a unique ID for each user. MySQL has this feature.

PHP - Problem with scalar value and memory leak

I am dealing with a very strange situation that has to do with the Warning: Cannot use a scalar value as an array and memory leak.
The script is very simple and I can not figure out the problem.
Code
$variants=array();
if($text)
{
$v=explode(",",$text);
if(is_array($v) && sizeof($v)>0)
{
foreach($v as $i=>$part)
{
$tmp=explode(":",$part);
list($thekey,$thevalue)=$tmp;
//$variants=array();
echo "<div>TYPE==".gettype($variants)."</div>";
echo $variants[$tmp[0]]=$tmp[1];
}
}
}
If I run the code above as stand alone is working fine. But when put it in my framework as small part behave very strange. I got a Warning: Cannot use a scalar value as an array and in order to solve it I added
$variants=array();
on the first line. When running the script the gettype returns ��� the first time and after that return integer.
If i uncomment $variants=array(); just before the gettype, it works. But of course I don't to get the whole array, only the last record return.
I parse my code to find out that the variables I use are declared before I even change all the variable names to stupids but no luck.
Trying to debug and tune the code where times that when running the script instead of see something in the screen the browser download the script instead and some other times I had memory leaks.
Can anyone point where or what to look for, or debug it and solve it?
Problem solved
Before run the code i was calling a function
$obj->draw($$id)
That was causing the problem
The solution
$value=$$id;
$obj->draw($value)
I dont know why this causing the problem.
If anyone has a theory please post it.

Trouble comparing two PHP variables

this may seem like a stupid question, but it is stumping me nontheless. I'm sure that the answer is something small. I think it's just one of those situations where I have been looking at the code for too long.
I am trying to compare two PHP variables to see if they are the same. As you can see below, I am comparing $verification_answer with strrev(date("Ymd")) which is today's date, reversed. So today, $verification_answer would be 31700102. Every time I try to do the comparison, however, the if statement executes (as a non-match).
$verification_answer = strrev(date("Ymd"));
if($verification != $verification_answer){
$failed .= "<h2>Attention:</h2><p>The verification code is incorrect. Please try again.</p>";
}
Can anyone see the issue? Thanks!
UPDATE: $verification is from HTML user input:
$verification = mysql_escape_string($_POST['verification']);
There's nothing wrong with the if statement. Display the two values and you should see some sort of difference:
var_dump($verification);
var_dump($verification_answer);
Perhaps $verification doesn't contain what you think it does, or you misspelled it earlier and assigned to a different variable, or...
I am comparing $verification_answer with strrev(date("Ymd"))
If that's actually what you intended to do, I think you messed up the name of the variable in the first line; it should be:
$verification = strrev(date("Ymd"));
If you accidentally overwrote the value of $verification_answer and used $verification in a comparison when it's undefined, the comparison will always be false. PHP will emit a warning, but if you have them disabled you won't see it
I must have spelled verification wrong somewhere...I copy and pasted 'verification' over each existing variable name and it fixed the problem. Thanks!

Categories