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
Ursine Catastrophe
Nov 9, 2009

It's a lovely morning in the void and you are a horrible lady-in-waiting.



don't ask how i know

Dinosaur Gum

Tiny Bug Child posted:

Are you looking for something like var_export()?

Is there any functional difference between var_export and serialize()? Just asking out of curiousity, since the latter is what I typically go to for stuff like this.

Adbot
ADBOT LOVES YOU

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

OriginalPseudonym posted:

Is there any functional difference between var_export and serialize()? Just asking out of curiousity, since the latter is what I typically go to for stuff like this.

serialize() returns a string that can be unserialize()'d later to get the same value. var_export() doesn't need an unserializing function because it returns actual PHP code, and you use it when you want to do something like have a PHP script that generates another PHP script. Using either function can be a sign that you're about to do something gross, especially if you're sticking a serialized object into a database or using var_export() in tandem with eval().

Bodhi Tea
Oct 2, 2006

seconds are secular, moments are mine, self is illusion, music's divine.
Anyone use the simple html dom library?
code:
<div> <span>foo</span> bar </div>
I can't find a good way to just get bar, excluding foo, in the above code.

baquerd
Jul 2, 2007

by FactsAreUseless
This seems like it would work:

code:
str_replace ( $divElement->first_child()->outertext , "" , $divElement->innertext )

the_cow_fan
May 12, 2008
Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

Sab669
Sep 24, 2009

the_cow_fan posted:

Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

I'm still a student, but a lot of job postings I see are all Wordpress, Joomla, or Drupal it seems.

dustin10
Jul 24, 2003

the_cow_fan posted:

Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

Symfony2 rocks. Hit me up on github if you have any questions. https://github.com/dustin10 or come chat on IRC; #symfony on freenode.

indulgenthipster
Mar 16, 2004
Make that a pour over

the_cow_fan posted:

Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

Yii is getting popular, I highly recommend checking it out (http://www.yiiframework.com/). I just made a point-of-sale system for a ski resort with it, and it went together smoothly and relatively quickly.

hmm yes
Dec 2, 2000
College Slice
There was just a bunch of PHP framework talk in the general web design thread

the_cow_fan
May 12, 2008
Thanks for the awesome suggestions, I'm currently messing around with Symonfy, I'll checkout Yii and have a read of the design thread.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror

the_cow_fan posted:

Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

It's hard to say what the "best framework" is. Asking people that question is probably going to get similar results as asking "What framework are you most familiar with?"

Give some thought to whether you really need a framework. PHP is actually pretty good on its own, and its frameworks tend to be big, lumbering, heavyweight things that force you into using their style. Rasmus Lerdorf (the dude who created PHP) is often critical of the larger frameworks, and wrote this article on a "no-framework MVC framework" to illustrate how you can write clean, well-organized code without using an external framework.

Personally, when I do need to use a framework, I like the Zend Framework. It's pretty good about letting you use only the parts you need.

e: apparently that link is down, here's google's cache in the meantime

indulgenthipster
Mar 16, 2004
Make that a pour over

Tiny Bug Child posted:

Rasmus Lerdorf (the dude who created PHP) is often critical of the larger frameworks, and wrote this article on a "no-framework MVC framework" to illustrate how you can write clean, well-organized code without using an external framework.

This is a really bad idea. You have to write everything from scratch, opening up the potential of security holes and exploits. Frameworks have teams of people testing these things all the time, something Uncle Joe's PHP Framework will lack. Also, you are going to have to think of every possible way you might want to expand this thing, as you can accidentally put your entire site in handcuffs with a bad design decision early on. I did this "roll your own" thing on 3 websites, and I regretted it after spending the time to learn Yii.

With a framework you get to spend more time worrying about how your website will look and behave instead of the architecture. The best frameworks give you the necessary tools to do that, and it's up to you to chose something like a heavyweight (PHPCake), or something very hands off (in my case, Yii).

edit: Also, didn't Rasmus argue against Object Oriented code at one point, and instead wrote a whole blog post about the benefits of procedural instead? He really isn't a good source of information.

Hammerite
Mar 9, 2007

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

VerySolidSnake posted:

edit: Also, didn't Rasmus argue against Object Oriented code at one point, and instead wrote a whole blog post about the benefits of procedural instead? He really isn't a good source of information.

I'm not saying Rasmus Lerdorf is a Good Programmer, but what's wrong with doing that? There's nothing wrong with procedural code.

Tiny Bug Child
Sep 11, 2004

Avoid Symmetry, Allow Complexity, Introduce Terror
Sure, if you're new to PHP and/or you're doing something that's already been done to death and/or performance isn't a concern, there's nothing wrong with using a framework. I'm just saying that a lot of developers consider copying over their MVC framework of choice to be the immutable step 0 of every project, but using a framework and using an MVC architecture are not only not prerequisites to writing good code but sometimes actively get in the way of getting things done.

VerySolidSnake posted:

edit: Also, didn't Rasmus argue against Object Oriented code at one point, and instead wrote a whole blog post about the benefits of procedural instead? He really isn't a good source of information.

I don't ever remember reading this, but I'm curious as to why you think it would discredit him. OOP isn't the one true way of doing things, it's just a paradigm with its own set of issues.

mewse
May 2, 2006

Tiny Bug Child posted:

OOP isn't the one true way of doing things

YES IT IS BLARRGGHGHG

indulgenthipster
Mar 16, 2004
Make that a pour over

Tiny Bug Child posted:

I don't ever remember reading this, but I'm curious as to why you think it would discredit him. OOP isn't the one true way of doing things, it's just a paradigm with its own set of issues.

I can't find the article, but I know it was one of the PHP "lead" guys. The article didn't just write about the benefits of procedural, but went on and on about why you shouldn't use object oriented. Everyone went up in arms about this. I'll try to find it and post it.

But even in his example "Write your own MVC" he is using .inc files. He doesn't even mention the fact once that these files do not parse PHP code, and if someone guesses the URL to that .inc they will see all of your config values. You could probably do some server configuration to prevent direct access to the files, but why would he even suggest a naming convention like that?

I used to only write procedural code for websites and every time I have to go back and make changes I want to slam my nuts in the door. When you are making larger websites OOP is the only way to do things.

klem_johansen
Jul 11, 2002

[be my e-friend]
I'm doing a REALLY simple PayPal function for a small organization. There's a redirect URL that updates the DB to note their payment, etc. Everything seems to work in theory. Testing is a bit weird, though.

I tried setting up a sandbox account to run null transactions (like you can with some other payment gateways) and it doesn't seem like it will work with the simplistic workflow we're using here. Is there a way to use a sandbox account to test payments on the site (rather than just log into the sandbox account page) or to simply cancel payment on the merchant side so it isn't credited?

Begby
Apr 7, 2005

Light saber? Check. Black boots? Check. Codpiece? Check. He's more machine than kid now.

klem_johansen posted:

I'm doing a REALLY simple PayPal function for a small organization. There's a redirect URL that updates the DB to note their payment, etc. Everything seems to work in theory. Testing is a bit weird, though.

I tried setting up a sandbox account to run null transactions (like you can with some other payment gateways) and it doesn't seem like it will work with the simplistic workflow we're using here. Is there a way to use a sandbox account to test payments on the site (rather than just log into the sandbox account page) or to simply cancel payment on the merchant side so it isn't credited?

You need two sandbox accounts, a buyer account and a seller account. Somewhere buried in the settings for the sandbox account you will be able to add a credit card. It will then generate a fake credit card number and add it to the account.

Since eBay started their whole x.commerce thing though, it seems all of their sandbox stuff has gotten even more buggy than usual. Yesterday I continually got redirected from the sandbox to pages on their taiwanese site. I think they are having some serious code management issues.

I had problems doing this before, and I got it working as best as I could, then finally I just ran a real credit card for a low purchase price and then cancelled it. Probably a good idea anyways for complete end to end testing.

KuruMonkey
Jul 23, 2004

Begby posted:

I had problems doing this before, and I got it working as best as I could, then finally I just ran a real credit card for a low purchase price and then cancelled it. Probably a good idea anyways for complete end to end testing.

This is essential, actually; PayPal's sandbox has never been an accurate duplication of their live system - for instance there are character encoding issues (causeing IPN to reject valid transactions, that if duplicated in the sandbox work fine) that exist ONLY in the live system; they are fun to deal with.

biochemist
Jun 2, 2005

Who says we don't have backbone?
Ok, so this is probably pretty simple but I've been stumbling.

I'm just trying to call a mySQL database with some information in it, put them in variables, and display them on a website. I've figured out how to access the database, but it's the SQL queries that are killing me. I'm using wordpress, and I want to pull my records based on the title of the blog post, which is usually returned with
code:
the_title()
So here's what works as far as a query:
code:
$query="SELECT * FROM daily_deals WHERE restaurant_name='Example Restaurant'";
and
code:
$restaurant_ID = 'Example Restaurant';
$query="SELECT * FROM daily_deals WHERE restaurant_name='$restaurant_ID'";
but what I can't figure out how to do is something like
code:
$query="SELECT * FROM daily_deals WHERE restaurant_name='the_title()'";
or
code:
$restaurant_ID = the_title();
$query="SELECT * FROM daily_deals WHERE restaurant_name='$restaurant_ID'";
Can anyone point me in the right direction? I guess this is a PHP question, but more of how it interacts with SQL. If I'm leaving out any important information just let me know.

Hammerite
Mar 9, 2007

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

biochemist posted:

Ok, so this is probably pretty simple but I've been stumbling.

...

Can anyone point me in the right direction? I guess this is a PHP question, but more of how it interacts with SQL. If I'm leaving out any important information just let me know.

The first thing you need to do is to learn about a type of website security vulnerability called SQL injection. (Here is the Wikipedia page. This is fun additional reading.) What happens if your article title is "Two meals for the price of one at O'Flanagan's Irish Pub"?

Having said that, if we completely ignore the issues posed by SQL injection (do not do this) the strings you are trying to form are not difficult to obtain. You would have two options: to create a new variable ($temp_variable) say, assigned the value returned by the_title(), and use variable interpolation as you had been doing:
code:
$temp_variable = the_title();
$query = "SELECT * FROM daily_deals WHERE restaurant_name = '$temp_variable'";
Alternatively you could just use string concatenation.
code:
$query = "SELECT * FROM daily_deals WHERE restaurant_name='".the_title()."'";
I provide these snippets so that you can understand ways of forming strings in PHP, as a consideration independent of working with SQL. Do not use either of these code snippets on your website. Neither of them is safe.

butt dickus
Jul 7, 2007

top ten juiced up coaches
and the top ten juiced up players
Use PDO. Once you've set up your connection (we'll call it $mysql), you can do this:
code:
$statement = $mysql->prepare("SELECT * FROM daily_deals WHERE restaurant_name = :name");
$statement->execute(array(":name" => the_title()));
while($row = $statement->fetch(PDO::FETCH_ASSOC)) {
//do stuff
}
PDO takes care of escaping based on the database, so you don't have to worry about it.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

biochemist posted:

Ok, so this is probably pretty simple but I've been stumbling.

I'm just trying to call a mySQL database with some information in it, put them in variables, and display them on a website. I've figured out how to access the database, but it's the SQL queries that are killing me.

It has been said by the two guys ahead of me, but I'm saying it a third time: Don't write your own SQL in PHP. It's like leaving your front and back door wide open before you go on vacation.

McGlockenshire
Dec 16, 2005

GOLLOCKS!
Given that you're using Wordpress, you might also want to look at the Wordpress database object and how it emulates safe parameter binding.

I wouldn't want to mix PDO and the Wordpress code at the same time.

Impotence
Nov 8, 2010
Lipstick Apathy

Lumpy posted:

It has been said by the two guys ahead of me, but I'm saying it a third time: Don't write your own SQL in PHP. It's like leaving your front and back door wide open before you go on vacation.
..while robbers are automated and trying every couple seconds

biochemist
Jun 2, 2005

Who says we don't have backbone?

Lumpy posted:

It has been said by the two guys ahead of me, but I'm saying it a third time: Don't write your own SQL in PHP. It's like leaving your front and back door wide open before you go on vacation.

Haha thanks everyone, I've heard of SQL attacks (hard not to with all of the press they got with lulzsec or whatever). I guess I was just rushing things to try to get some functionality up, but it makes much more sense to build that all in as you go than try to come back and patch things up.

I'll take a look at all of your suggestions, and I'm sure I'll be back with some other inane questions. Thanks again!

Oh, a little edit: I could skip the SQL database entirely and it might even work better if I could have my page template contain all of the formatting and variable calls, but assign them in the actual post. The post content is called with
code:
<?php the_content(); ?>
But it doesn't seem to pass variables back to the page template (scope problem?). Wordpress doesn't natively support PHP in the posts, but I found a little plugin that lets me insert PHP between tags. I'm by no means an expert in this stuff, just confident enough to give different things a try until they kinda work, and the other guy working on this has no idea what he's doing. It might be easier if we could have it organized that way rather than mess around with SQL and the security issues that come with it.

biochemist fucked around with this message at 07:38 on Dec 5, 2011

Mug
Apr 26, 2005
Anyone have any idea why, when I do file_get_contents on this image (hopefully you can see it without an active session): http://media.allinteractive.com.au/content/55c363911af776eb2f93ae665935fd6c/4edc7691/image/4/b18ec27a-a614-11df-af95-001a6465ada8.jpg

And copy it into a MySQL blob, and then I echo it back to the browser out of the database, I get this?



I assume there's some character in the JPEG raw that is breaking the MySQL insertion and inserting only half of it, but if I encapsulate the query in mysql_real_escape_string or addslashes it makes no difference. Anyone able to help?

edit: also, if I encapsulate it with addslashes, then output it with stripslashes, it goes spastic and it's just a mess. What the hell?

edit 2: Fixed it, didn't realize BLOB had a 65535byte limit. Thanks goons!

Mug fucked around with this message at 08:53 on Dec 5, 2011

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

biochemist posted:


Oh, a little edit: I could skip the SQL database entirely and it might even work better if I could have my page template contain all of the formatting and variable calls, but assign them in the actual post. The post content is called with
code:
<?php the_content(); ?>

There is an excellent WordPress thread and I'm sure they will be able to help you do exactly what you want in WP-eqsue fashion using it's internal way of getting things.

Psycho Penguin
Aug 26, 2008

the_cow_fan posted:

Whats the best framework to use nowadays? a lot of people seem to be against cake for various reasons, I've been mainly looking at symfony.

I have made quite a few projects using CodeIgniter, and I really enjoy it. It's very simple to learn.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!
Am I doing this wrong?

It's getting to be loving retarded to do a simple task.

php:
<?
// load string into a DOM object as HTML
$dom = new DOMDocument();
@$dom->loadHTML($result);

// create title tag
$title = $dom->createElement('title', '[redacted]');

// create viewport meta tag
$metaViewport = $dom->createElement('meta');
$metaViewportName = $dom->createAttribute('name');
$metaViewportContent = $dom->createAttribute('content');
$metaViewportName->value = 'viewport';
$metaViewportContent->value = 'width=device-width';
$metaViewport->appendChild($metaViewportName);
$metaViewport->appendChild($metaViewportContent);

// create reset styles css link
$resetCSSlink = $dom->createElement('link');
$resetCSSHREF = $dom->createAttribute('href');
$resetCSSREL = $dom->createAttribute('rel');
$resetCSSTYPE = $dom->createAttribute('type');
$resetCSSHREF->value = '/css/reset.css';
$resetCSSREL->value = 'stylesheet';
$resetCSSTYPE->value = 'text/css';
$resetCSSlink->appendChild($resetCSSHREF);
$resetCSSlink->appendChild($resetCSSREL);
$resetCSSlink->appendChild($resetCSSTYPE);


// create custom styles css link
$customCSSlink = $dom->createElement('link');
$customCSSHREF = $dom->createAttribute('href');
$customCSSREL = $dom->createAttribute('rel');
$customCSSTYPE = $dom->createAttribute('type');
$customCSSHREF->value = '/css/styles.css';
$customCSSREL->value = 'stylesheet';
$customCSSTYPE->value = 'text/css';
$customCSSlink->appendChild($customCSSHREF);
$customCSSlink->appendChild($customCSSREL);
$customCSSlink->appendChild($customCSSTYPE);

// insert elements into head
$head = $dom->getElementsByTagName('head')->item(0);
$head->insertBefore($customCSSlink, $head->firstChild);
$head->insertBefore($resetCSSlink, $head->firstChild);
$head->insertBefore($metaViewport, $head->firstChild);
$head->insertBefore($title, $head->firstChild);

// output DOM object as HTML
echo $dom->saveHTML();
?>

McGlockenshire
Dec 16, 2005

GOLLOCKS!
I see PHP Simple HTML DOM recommended quite a bit for such things.

Scaevolus
Apr 16, 2007

IT Guy posted:

Am I doing this wrong?

It's getting to be loving retarded to do a simple task.
Manipulating the DOM directly sucks a lot.

If you're okay with XHTML instead of HTML, use XMLWriter instead. It's stream-based, so your code ends up looking a lot nicer:

php:
<?
$w=new XMLWriter();
$w->openMemory();
$w->startDocument('1.0','UTF-8');
$w->startElement("root");
    $w->writeAttribute("ah", "OK");
    $w->text('Wow, it works!');
$w->endElement();
echo $w->outputMemory(true);
?>

IT Guy
Jan 12, 2010

You people drink like you don't want to live!

Scaevolus posted:

Manipulating the DOM directly sucks a lot.

If you're okay with XHTML instead of HTML, use XMLWriter instead.

I was unaware of XMLwriter, but it looks like it might work out better for me, thank you.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!
I have a question regarding sanitizing data.

When doing something like this:

code:
<p><?php echo htmlspecialchars($array[$key], ENT_QUOTES, 'utf-8'); ?></p>
Do I also have to wrap $key in htmlspecialchars()?

Hammerite
Mar 9, 2007

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

IT Guy posted:

I have a question regarding sanitizing data.

When doing something like this:

code:
<p><?php echo htmlspecialchars($array[$key], ENT_QUOTES, 'utf-8'); ?></p>
Do I also have to wrap $key in htmlspecialchars()?

It is the contents of $array[$key] that you are outputting to the browser. The browser does not see the value of $key. So no.

To give an example:

code:
$array = array('<butt>' => 'fart');
$key = '<butt>';
If you apply htmlspecialchars() to key then you get '&lt;butt&gt;'. '&lt;butt&gt;' is not a key in the array $array, so if you do this:

code:
echo htmlspecialchars($array[htmlspecialchars($key)]);
you will echo nothing and you will get a notice about an undefined array index.

IT Guy
Jan 12, 2010

You people drink like you don't want to live!

Hammerite posted:

It is the contents of $array[$key] that you are outputting to the browser. The browser does not see the value of $key. So no.

Makes sense, thank you.

DarkLotus
Sep 30, 2001

Lithium Hosting
Personal, Reseller & VPS Hosting
30-day no risk Free Trial &
90-days Money Back Guarantee!
Let's say I have more than 2 arrays of supposedly unique values. How would I go about finding out if any one value in an array exists in the rest of the arrays? I am looking for a fairly fast way to do this without having to loop through each array manually and see if the value exists in any of the other arrays. To clarify, I'm hoping to avoid using in_array(), I'm also only interested in the whether "user" is duplicated.

rough example:
php:
<?
array (
    0 => array ('user' => 'jdoe','email' => NULL,),
    1 => array ('user' => 'tsmith','email' => 'tomsmith@gmail.com')
)

array (
    0 => array ('user' => 'pmcallister','email' => 'pmc@test.com'),
    1 => array ('user' => 'nnancy','email' => 'nnancy@gmail.com')
)

array (
    0 => array ('user' => 'jdoe','email' => 'jdoe@pmail.com'),
    1 => array ('user' => 'griviera','email' => 'george@poop.com')
)

array (
    0 => array ('user' => 'rmurphy','email' => 'rachel@murphygroup.com'),
    1 => array ('user' => 'nnancy','email' => 'nnancy@gmail.com')
)
?>
You can see a couple of the users are duplicated.

DarkLotus fucked around with this message at 02:37 on Dec 24, 2011

SubG
Aug 19, 2004

It's a hard world for little things.
Is there better documentation of mcrypt internals than can be found on php.net?

I'm trying to write some C code using OpenSSL to decrypt some strings that have been encrypted using something like:
php:
<?
$plaintext = '12345';
$key = 'secret';

$enc = base64_encode(mcrypt_encrypt(MCRYPT_BLOWFISH, $key, $plaintext,
        MCRYPT_MODE_ECB));

printf("<P>encrypted string is \"%s\"</P>", $enc);
?>
This outputs the string as "8l1nTEpn1Xs=", which OpenSSL doesn't decrypt. E.g., from the command line:
code:
# echo "8l1nTEpn1Xs=" | openssl enc -bf-ecb -d -a -nosalt -k "secret"
bad decrypt
3154:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:330:
...and, for that matter, openssl produces a different cypher string:
code:
# echo "12345" | openssl enc -bf-ecb -e -a -nosalt -k "secret" 
OX8xtRHb+H8=
So I'm guessing there's something funny going on with different block sizes, padding, or something. Unfortunately, the mycrypt source in php isn't the most spectacularly readable code, so I'm hoping for some documentation that's a little more detailed on the internals than the stuff on php.net.

McGlockenshire
Dec 16, 2005

GOLLOCKS!
echo in the shell always appends a newline, which can easily throw things off. Try echo -n instead?

code:
[mcg@mcg-workstation ~]$ echo -n "12345" | openssl enc -bf-ecb -e -a -nosalt -k "secret"
lFaLrQWQU5U=
Hmm, or not.

Adbot
ADBOT LOVES YOU

SubG
Aug 19, 2004

It's a hard world for little things.

McGlockenshire posted:

echo in the shell always appends a newline, which can easily throw things off. Try echo -n instead?

code:
[mcg@mcg-workstation ~]$ echo -n "12345" | openssl enc -bf-ecb -e -a -nosalt -k "secret"
lFaLrQWQU5U=
Hmm, or not.
Yeah, adding a newline into any string less than some number of bytes when you're using base64 encoding is actually part of the spec; it's some MIME thing. If you're tinkering around with OpenSSL in C you can disable this behaviour by setting the flag BIO_FLAGS_BASE64_NO_NL.

This could explain why the two ciphertexts don't match---assuming everything else was equal---but it doesn't explain why a ciphertext produced by mcrypt can't be decrypted by OpenSSL at all (as opposed to just decrypting plus or minus an extraneous newline). Which is my real problem.

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