Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
KarmaticStylee
Apr 21, 2007

Aaaaaughibbrgubugbugrguburgle!
Sorry if this has been asked but

Do any of you use/recommend:
http://www.phpobjectgenerator.com/

I've messed with CakePHP but it seems a bit much for me and this solution seemed pretty good but I'm pretty new to OOP PHP programming

Adbot
ADBOT LOVES YOU

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Munkeymon posted:

Is there a good way to handle nodes with perios in their names when using simplexml?

code:
<this.will.break.poo poo>
  <oh.crap>Haha, loser.</oh.crap>
</this.will.break.poo poo>
Without leaning on xpath all over the place?

php:
<?

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<this.will.break.poo poo>
  <oh.crap>Haha, loser.</oh.crap>
</this.will.break.poo poo>
XML;

$xml = new SimpleXMLElement($xmlstr);

echo $xml->{"oh.crap"};

?>
When in doubt, play with curly braces.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



duz posted:

php:
<?

$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<this.will.break.poo poo>
  <oh.crap>Haha, loser.</oh.crap>
</this.will.break.poo poo>
XML;

$xml = new SimpleXMLElement($xmlstr);

echo $xml->{"oh.crap"};

?>
When in doubt, play with curly braces.

Thanks, that's much better, but I was hoping there was an option to convert characters in nodes to something variable-friendly in order to do something like $xml->this__will__break__shit->oh__crap.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Munkeymon posted:

Thanks, that's much better, but I was hoping there was an option to convert characters in nodes to something variable-friendly in order to do something like $xml->this__will__break__shit->oh__crap.

If they did that, they've have collision issues. You can extend SimpleXML to do that for you if you really want it.

Munkeymon
Aug 14, 2003

Motherfucker's got an
armor-piercing crowbar! Rigoddamndicu𝜆ous.



duz posted:

If they did that, they've have collision issues. You can extend SimpleXML to do that for you if you really want it.

I understand they could have collision issues, but if they let you pass in an array to use to map characters or something then it would just be the user's problem and they wouldn't have to worry about it :)

I'm going to have to live with slightly uglier code with my deadline.

Golbez
Oct 9, 2002

1 2 3!
If you want to take a shot at me get in line, line
1 2 3!
Baby, I've had all my shots and I'm fine
Tell me about error handling. Right now, I use a mixture of "return false" and "if (!$functionthatreturnedfalse) die ('error message goes here')". Is this sufficient? Should I use trigger_error? And when should I use exceptions, only for vastly important things? I've suddenly gotten into the object oriented end of things and my old methods for dealing with errors (which was basically "mysql_query($query) or die (mysql_error())") might not be good enough anymore.

When I catch an Exception, doesn't the rest of the program continue to execute?

Zorilla
Mar 23, 2005

GOING APE SPIT

Golbez posted:

When I catch an Exception, doesn't the rest of the program continue to execute?

I generally see exceptions handled when one of the possible outcomes of the function you're using is having it throw an exception. For instance, maybe you're using a function that reads a file or makes a connection, but throws an exception when it fails. You'll want to handle each of these situations since things don't work perfectly all the time in the real world. If a file can't be read or a connection can't be made, it's much better to give the user a friendly error message rather than having it puke right where the exception happens.

In my amateurish opinion, if you're writing object methods, it's probably up to your judgment if you want it to either return false, present a warning, or cause a fatal error.

Zorilla fucked around with this message at 03:29 on Jan 30, 2009

agscala
Jul 12, 2008

I want to make a website that would take an RSS feed, parse the information and do things like notify users when a specific event occurs so they wouldn't need to wade though a bunch of stuff they don't want. What libraries would be good for doing something along these lines?

Also, on an unrelated note, I've been doing development on XAMPP, and I wanted to fiddle around with PHP's mail(). I tried just running the mail server that's included with XAMPP but that seems to be not enough to be able to send mail. Could anyone point me in the right direction towards setting it up?

Zorilla
Mar 23, 2005

GOING APE SPIT

agscala posted:

I want to make a website that would take an RSS feed, parse the information and do things like notify users when a specific event occurs so they wouldn't need to wade though a bunch of stuff they don't want. What libraries would be good for doing something along these lines?

Parse XML? SimpleXML or DOMDocument.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Session security

I'm working on an application that needs to support session data for users with cookies disabled. I am concerned about the situation where a user is logged in, has cookies disabled (so PHPSESSID is being passed in the url) and shares a link to their current page with someone else (emails, IMs, bookmarks, etc). Now the other user can gain access to that user's account.

I know about checking for IP address and user agent consistency, but is there a more secure and less arbitrary way of doing this?

Smoetihng Awufl
Jul 1, 2008

CaTs rULe DoGz DrOoL
its true and you know it
How come you can't just ask them to enable cookies?

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Because some users will be accessing the application from offsite server machines that are locked down (e.g., high privacy settings in browsers).

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker
Echo the sessid into a hidden form on every page? That's an option, although it's just as secure as a sessid in the url. You should look into https and its certificates if you really want to be safe.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
How would HTTPS change the session situation?

KuruMonkey
Jul 23, 2004

supster posted:

How would HTTPS change the session situation?

It wouldn't.

As far as I know you need to; lock sessions to a single IP, and then make the session timeout short (like 10 minutes) and of course the only piece of info the session should be causing to ping back and forth from client to server and back is a session id.

ssl just obscures the session id while in transit.

the littlest prince
Sep 23, 2006


I'm a total php noob, and I want to do some simple site-scraping to make a page for my own personal use. What I want to do is run a search on a couple of craigslist sites (e.g. new york, los angeles, and denver) and then display the results all on one page.

So, step 1 is to get the contents of one craigslist site so I can tell whether the way I'm going about pulling a page is working. Supposedly, file_get_contents should do the trick.

php:
<?
$a = file_get_contents("http://dayton.craigslist.org/");
if ($a == false)
   echo "no good";
else
   echo "ok";
?>
But, this isn't working, as it's always false. I've tried pretty much every example on the page I linked above, but nothing stuck. So I also tried the example on this page (#1), but it didn't work either. Any idea what I'm doing wrong?

waffle iron
Jan 16, 2004

the littlest prince posted:

I'm a total php noob, and I want to do some simple site-scraping to make a page for my own personal use. What I want to do is run a search on a couple of craigslist sites (e.g. new york, los angeles, and denver) and then display the results all on one page.

So, step 1 is to get the contents of one craigslist site so I can tell whether the way I'm going about pulling a page is working. Supposedly, file_get_contents should do the trick.

php:
<?
$a = file_get_contents("http://dayton.craigslist.org/");
if ($a == false)
   echo "no good";
else
   echo "ok";
?>
But, this isn't working, as it's always false. I've tried pretty much every example on the page I linked above, but nothing stuck. So I also tried the example on this page (#1), but it didn't work either. Any idea what I'm doing wrong?
Craigslist doesn't like people doing mash ups with their stuff. You probably need to have PHP send a UserAgent that would come from a browser.

See http://www.php.net/manual/en/filesystem.configuration.php#ini.user-agent

PainBreak
Jun 9, 2001
I'm beginning to realize, PHP just ain't my thing...

I have a list.txt that looks like this:

j2929j
l2l32l
fa98a0
8fa9a9
nb1kjk

And a bit o' php that looks like this:

<?php
$entry = 'fa98a0';
$lines = file('list.txt');
trim($lines);
foreach ($lines as $line)
{
if ($line==$entry)
{
echo "It's A Dupe!";
}
}
?>

It seems to think that fa98a0 != fa98a0 which blows my loving mind. It's only because it's in the middle of the array, though. If I use the very last entry in list.txt it knows that it's the same, and all is well. I've also tried in_array and it functioned similarly. What am I doing wrong?

PainBreak fucked around with this message at 21:29 on Jan 31, 2009

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

KuruMonkey posted:

It wouldn't.

As far as I know you need to; lock sessions to a single IP, and then make the session timeout short (like 10 minutes) and of course the only piece of info the session should be causing to ping back and forth from client to server and back is a session id.

ssl just obscures the session id while in transit.

Is setting session.gc_maxlifetime to a short amount good enough, or should the timoeut check be done explicitely?

MrMoo
Sep 14, 2000

PainBreak posted:

php:
<?php
$entry 'fa98a0';
$lines file('list.txt'FILE_IGNORE_NEW_LINES);
foreach ($lines as $line)
{
if ($line==$entry)
{
echo "It's A Dupe!";
}
}
?>


Fixed it for you.

slaphappy
Aug 4, 2007
edit: beaten

Roctor
Aug 23, 2005

The doctor of rock.

PainBreak posted:

I'm beginning to realize, PHP just ain't my thing...

I have a list.txt that looks like this:

j2929j
l2l32l
fa98a0
8fa9a9
nb1kjk

And a bit o' php that looks like this:

<?php
$entry = 'fa98a0';
$lines = file('list.txt');
trim($lines);
foreach ($lines as $line)
{
if ($line==$entry)
{
echo "It's A Dupe!";
}
}
?>

It seems to think that fa98a0 != fa98a0 which blows my loving mind. It's only because it's in the middle of the array, though. If I use the very last entry in list.txt it knows that it's the same, and all is well. I've also tried in_array and it functioned similarly. What am I doing wrong?

The reason it doesn't work is because you're actually comparing 'fa98a0' with 'fa98a0\n'. Because of the new line character at the end of the line in the file it's not returning true.

edit: beaten. thought mrmoo was just being snarky by saying he 'fixed' your post by adding the code tags.

Lankiveil
Feb 23, 2001

Forums Minimalist
I've got some strings that need to be inserted into the database via an UPDATE command. However, the strings may or may not already be escaped (don't ask). For instance, I might have "don't go not don't" or "don/'t go not don/'t" passed to my module.

Is there any reliable way to make sure that apostrophes have only one slash, and are not double-slashed like in this thread title?

Standish
May 21, 2001

Lankiveil posted:

I've got some strings that need to be inserted into the database via an UPDATE command. However, the strings may or may not already be escaped (don't ask). For instance, I might have "don't go not don't" or "don/'t go not don/'t" passed to my module.

Is there any reliable way to make sure that apostrophes have only one slash, and are not double-slashed like in this thread title?
No, you cannot tell the difference between "\\\' because it's been double-escaped" and "\\\' because that's what the user actually entered".

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

Standish posted:

No, you cannot tell the difference between "\\\' because it's been double-escaped" and "\\\' because that's what the user actually entered".

Yep, but if you're willing to assume that your users will never input a '\', then you can do something like this:
php:
<?
function stripallslashes($str)
{
    if(strpos($str, '\') !== FALSE)
        $str = stripallslashes(stripslashes($str));
    return $str;
}
?>
If performance is an issue (i.e., you're doing this to thousands of very large strings that have been escaped many times) you can greatly improve this algorithm.

supster fucked around with this message at 14:25 on Feb 1, 2009

Supervillin
Feb 6, 2005

Pillbug

Lankiveil posted:

I've got some strings that need to be inserted into the database via an UPDATE command. However, the strings may or may not already be escaped (don't ask). For instance, I might have "don't go not don't" or "don\'t go not don\'t" passed to my module.

Is there any reliable way to make sure that apostrophes have only one slash, and are not double-slashed like in this thread title?

Yep, do this to whatever your input variable is before you escape stuff yourself:
code:
$input = preg_replace('/\\\\+\'/', "'", $input);
That'll take care of previously escaped, double escaped, even quintuple escaped apostrophes, which you can then singly escape yourself.

Or if you want to unescape quotes and apostrophes:
code:
$input = preg_replace('/\\\\+(\'|")/', '\\1', $input);
Both of those will not get rid of slashes that are not attached to apostrophes or quotes. For example, an input like d\\on\\'t will become d\\on't. If that's a problem, you can get rid of ALL backslashes with this:
code:
$input = str_replace('\\', '', $input);

duck monster
Dec 15, 2004

Just a quick reminder how utterly broken this language can be...

code:
 <?
  print (5 + (int)$nuts) . "<br>";
  print (5 + $nuts);

?>
Outputs

quote:

5
5

:psyduck:

How the gently caress is x + Null = x??????

x + Null = Null!!!!!!!!!!!!!

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

duck monster posted:

Just a quick reminder how utterly broken this language can be...

code:
 <?
  print (5 + (int)$nuts) . "<br>";
  print (5 + $nuts);

?>
Outputs


:psyduck:

How the gently caress is x + Null = x??????

x + Null = Null!!!!!!!!!!!!!
Uh... there's a lot of things wrong with PHP, but this isn't one of them.

duck monster
Dec 15, 2004

supster posted:

Uh... there's a lot of things wrong with PHP, but this isn't one of them.

How is "Incorrectly evaluates null" not a problem. When your language cant do basic maths properly, yeah man, its a problem, and this problem has caused our team about a week and a half running around in circles looking for very obscure bugs.

The example I posted has PHP just inventing that Null = zero. Null is never equal to zero, otherwise it would not be null, it'd be zero.

The top line is *sort of* correct, because I'm using type forcing to specify to handle null as an integer. Well it might as well be zero, although 42 is equally valid.

The second line is an outright bug.

duck monster fucked around with this message at 04:51 on Feb 2, 2009

Hammerite
Mar 9, 2007

And you don't remember what I said here, either, but it was pompous and stupid.
Jade Ear Joe

duck monster posted:

How is "Incorrectly evaluates null" not a problem. When your language cant do basic maths properly, yeah man, its a problem, and this problem has caused our team about a week and a half running around in circles looking for very obscure bugs.

The example I posted has PHP just inventing that Null = zero. Null is never equal to zero, otherwise it would not be null, it'd be zero.

The top line is *sort of* correct, because I'm using type forcing to specify to handle null as an integer. Well it might as well be zero, although 42 is equally valid.

The second line is an outright bug.

Surely if the first example is all right then the second example is as well. By using + after 5 it would seem that you're inviting PHP to perform integer (well, maybe even real number) addition, in which case it seems quite reasonable that it makes no difference whether you put (int) in there or not; it ought probably to convert whatever it sees after the + to an integer, regardless.

Also, you're describing something as "basic maths" which to me (algebraic topologist in training) is really some kind of point of computer science orthodoxy, not truly mathematics, but w/e.

duck monster
Dec 15, 2004

Hammerite posted:

Surely if the first example is all right then the second example is as well. By using + after 5 it would seem that you're inviting PHP to perform integer (well, maybe even real number) addition, in which case it seems quite reasonable that it makes no difference whether you put (int) in there or not; it ought probably to convert whatever it sees after the + to an integer, regardless.

Also, you're describing something as "basic maths" which to me (algebraic topologist in training) is really some kind of point of computer science orthodoxy, not truly mathematics, but w/e.

Sure. Let me explain.

Null means "I dont have a value for this." , or alternatively "Not a number" (Some languages have different signifiers for null, and not-a-number), so if Y = null, then X + Y = null because if you dont have a value for Y , then you dont have a value for X. Same with subtraction, multiplication and in fact pretty much any operation, with a few knobs.

Now when you type cast to an int your actually saying , you MUST return this as an integer. The truly correct answer is to explode in flames and drop an exception, but assuming you don't actually want that (int) Null might as well be a good way of saying "Treat Null as zero".

PainBreak
Jun 9, 2001

MrMoo posted:

Fixed it for you.

Huh... Yeah I ended up figuring it out, and realized that trim($blah); doesn't really do anything...but $blah = trim($blah); does!

What you added, however, is quite a bit more elegant. Thanks!

PainBreak fucked around with this message at 05:37 on Feb 2, 2009

gibbed
Apr 10, 2006

duck monster posted:

Sure. Let me explain.

Null means "I dont have a value for this." , or alternatively "Not a number" (Some languages have different signifiers for null, and not-a-number), so if Y = null, then X + Y = null because if you dont have a value for Y , then you dont have a value for X. Same with subtraction, multiplication and in fact pretty much any operation, with a few knobs.

Now when you type cast to an int your actually saying , you MUST return this as an integer. The truly correct answer is to explode in flames and drop an exception, but assuming you don't actually want that (int) Null might as well be a good way of saying "Treat Null as zero".
Or you can just live with PHP's implicit casting of your null value to 0. It's not that bad.

duck monster
Dec 15, 2004

gibbed posted:

Or you can just live with PHP's implicit casting of your null value to 0. It's not that bad.
Except that its wrong and causes serious loving headaches.

gibbed
Apr 10, 2006

duck monster posted:

Except that its wrong and causes serious loving headaches.
In what situation would you find '1 + null = null' useful?

sonic bed head
Dec 18, 2003

this is naturual, baby!

duck monster posted:

Sure. Let me explain.

Null means "I dont have an answer for this." , so if Y = null, then X + Y = null because if you dont have a value for Y , then you dont have a value for X. Same with subtraction, multiplication and in fact pretty much any operation, with a few knobs.

Now when you type cast to an int your actually saying , you MUST return this as an integer. The truly correct answer is to explode in flames and drop an exception, but assuming you don't actually want that (int) Null might as well be a good way of saying "Treat Null as zero".

I feel like this is only a RDMS thing. Outside of databases, I've never heard of that requirement. Javascript definitely has no problem with adding numbers to nulls.

duck monster
Dec 15, 2004

gibbed posted:

In what situation would you find '1 + null = null' useful?

Well i've been working with a reverse chain induction algorithm where 1 + null should always evaluate to null, because it means we dont have the pieces to evaluate the solution correctly.

Even if its not usually that useful, its yet another example of PHP breaking itself to hold innexperienced coders hands.

Take the old php3/4 register globals problem, you'd actually eliminate half of the problems it caused by treating null as null, because at least you'd be treating a lack of data as a lack of data. "Insufficient data" means "Insufficient data", if you treat it as zero, you clearly are not "getting" algebra.

And if Javascript treats nulls as zero, then javascript is broken too, although by and large, most of us already knew that.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

duck monster posted:

Except that its wrong and causes serious loving headaches.
It's not wrong, it is how weakly typed dynamic typing languages are supposed to behave. Numeric types cannot have a null value. Obviously this is specified by the language, but I cannot think of a single language that allows for null valued numeric types. If you're complaining about this characteristic of PHP then you might as well be complaining about C, C++, Java, Python, Ruby, C#, etc.


edit:

duck monster posted:

And if Javascript treats nulls as zero, then javascript is broken too, although by and large, most of us already knew that.
Can you point out a language that doesn't treat nulls as zero when strictly talking about an integer type?

supster fucked around with this message at 05:47 on Feb 2, 2009

duck monster
Dec 15, 2004

supster posted:

It's not wrong, it is how weakly typed dynamic typing languages behave. Numeric types cannot have a null value. Obviously this is specified by the language, but I cannot think of a single language that allows for null valued numeric types. If you're complaining about this characteristic of PHP then you might as well be complaining about C, C++, Java, Python, Ruby, C#, etc.

Wrong, and remember that Python does a fair bit of type cohercion via magic methods where possible.

code:
>>> x = None
>>> print x
None
>>> print x + 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'NoneType' and 'int'
>>>
This is correct behavior

Also a bit of research is telling me javascript doesn't behave the way many here seem to think it behaves.

http://saladwithsteve.com/2008/02/javascript-undefined-vs-null.html

edit:
>> alert(1 + y)
y is not defined

dropping an exception is correct behavior.

Note that Javascripts diferentiates Null, undefined and NaN, which is even more correct, so..

z = undefined
>> z + 1
NaN

duck monster fucked around with this message at 05:53 on Feb 2, 2009

Adbot
ADBOT LOVES YOU

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

sonic bed head posted:

I feel like this is only a RDMS thing. Outside of databases, I've never heard of that requirement. Javascript definitely has no problem with adding numbers to nulls.
And this is because of how ANSI SQL nulls are defined. Read here for more info: http://luke.breuer.com/time/item/ANSI_SQL_nulls/94.aspx.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply