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
fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

MrMoo posted:

That's generally what AJAX is for.

Why make the extra request if I'm rendering other things just like it already there in the template though?

Adbot
ADBOT LOVES YOU

Hammerite
Mar 9, 2007

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

486 posted:

How are you storing the time? What format? You likely need to convert your old times and save your new times with one of PHP's many handy date functions

http://us2.php.net/manual/en/function.gmdate.php
Thanks for responding. Your post prompted me to do a couple of experiments, which revealed that the culprit of my problem is actually not PHP at all as I was assuming - it's MySQL. I'm therefore going to post in the general MySQL questions thread to ask for help.

Uncle Marx
Jan 16, 2006

How do I round up a number if a division doesn't have an integer as the result?

Examples:
8 stays 8
7.58333 becomes 8
7.00001 becomes 8

The only thing I have is checking the result with !is_int(), but I have no idea how to round the number up to the next integer.

MrMoo
Sep 14, 2000

fletcher posted:

Why make the extra request if I'm rendering other things just like it already there in the template though?

Nobody cares, whichever is the easiest to implement and look after is the correct answer.

Tad Naff
Jul 8, 2004

I told you you'd be sorry buying an emoticon, but no, you were hung over. Well look at you now. It's not catching on at all!
:backtowork:

Uncle Marx posted:

How do I round up a number if a division doesn't have an integer as the result?

Examples:
8 stays 8
7.58333 becomes 8
7.00001 becomes 8

The only thing I have is checking the result with !is_int(), but I have no idea how to round the number up to the next integer.

ceil()

Uncle Marx
Jan 16, 2006

FeloniousDrunk posted:

ceil()
Thanks :)

KuruMonkey
Jul 23, 2004
There are some pages on php.net that, if we aspire to being any form of php programmer, we should all at least scan through to look at what the functions in each list do:

http://php.net/manual/en/ref.math.php

http://php.net/manual/en/ref.strings.php

http://php.net/manual/en/ref.array.php

We've decided to dine at the PHP restaurant; we might as well flip through the menu.

Nobody needs to memorise all of these (I check the parameter order of strpos and in_array probably once a month - check them out to see why I have to do this!). I also infrequently browse through the lists to see what functions I've been ignoring or reinventing. But having scanned it all at least once, means that you'll at least remember that there were LOTS of array sorting functions, not just a couple - then you can go see if one does what you really need right now...

A quick scan through will tell you all the neat functions you have available to do the really common things. Or it will tell you what PHP calls your favourite functions from other languages. Or in some cases it'll introduce you to really weird functions that solve obscure corner cases.

Seriously; its worth reading through what all the various XsortYZ functions for arrays actually do. In some cases just to think to yourself; who the christ actually NEEDED that?

There are many things you can complain about regarding PHP. My favourite at the moment is that class constants have to be explicitly scope-resolved INSIDE the defining class! (seriously; no default to local scope? what?)

But lack of basic docs isn't one of them. Awkwardly arranged docs isn't either.

I'm not raging against anyone's questions, honest. Just occurred to me that those 3 urls can solve maybe 20% of 'is there a function for ...' questions in this thread?

Also:
php:
<?
class Numpty
{
  const MYCONST = 3;

  public $a = MYCONST; // failure!

  public $b = self::MYCONST; // WHY?!? Why must I do this?
}
?>

Munkeymon
Aug 14, 2003

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



Can anyone think of a good reason that putting this:
php:
<?
if(!function_exists('udbError_sendErrorReply'))
{
    function udbError_sendErrorReply($errMsg,$error_id){
        include_once('udbGlobal.php');
        $reply = $xmlHeader;
        $reply .= "<USER id=\"\">\n";
        $reply .= "<STATUS>ERROR</STATUS>\n";
        $reply .= "<ERROR>$error_id</URL>\n";
        $reply .= "<ERROR_MESSAGE>$errMsg</ERROR_MESSAGE>\n";
        $reply .= "<DEBUG></DEBUG>\n";
        $reply .= "</USER>\n";
        print $reply;
        return $reply;
    }
}
?>
in a script would cause PHP to tell me I'm trying to redefine udberror_senderrorreply, which does exist in a different file, but one that is not always included. I mean, is there some stupid gotcha in the way function_exists works?

Please do me a favor and ignore anything weird going on in the actual function I pasted up there thanks.

Munkeymon fucked around with this message at 15:39 on Apr 8, 2009

epswing
Nov 4, 2003

Soiled Meat
I've got a block of PHP that needs to avoid concurrent execution. I've thought about writing a small library which uses flock to LOCK_EX a lockfile, and then LOCK_UN when done, so other threads* will wait to acquire their LOCK_EX on said lockfile. It seems PHP will indeed block on a call to flock with a LOCK_EX parameter.

http://ca3.php.net/manual/en/function.flock.php
http://www.tuxradar.com/practicalphp/8/11/0

Is this a Bad Idea (tm)?

* When I say "other threads", I don't mean to imply that PHP itself is multi-threaded in the traditional sense, but web programming is inherently concurrent in that two users clicking the same button at the same time may cause the web server to execute the same script twice, and have both instances running concurrently.

Safety Shaun
Oct 20, 2004
the INTERNET!!!1
I have some returned XML data in $xml using simplexml_load_file() and I want to extract $someTitle from it from $xml->a->b->c->d->Title , how would I go about doing so?

Thanks in advance

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Safety Shaun posted:

I have some returned XML data in $xml using simplexml_load_file() and I want to extract $someTitle from it from $xml->a->b->c->d->Title , how would I go about doing so?

Thanks in advance

http://us.php.net/manual/en/function.simplexml-element-xpath.php

You are welcome.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

Munkeymon posted:

Can anyone think of a good reason that putting this:
php:
<?
if(!function_exists('udbError_sendErrorReply'))
{
    function udbError_sendErrorReply($errMsg,$error_id){
        include_once('udbGlobal.php');
        $reply = $xmlHeader;
        $reply .= "<USER id=\"\">\n";
        $reply .= "<STATUS>ERROR</STATUS>\n";
        $reply .= "<ERROR>$error_id</URL>\n";
        $reply .= "<ERROR_MESSAGE>$errMsg</ERROR_MESSAGE>\n";
        $reply .= "<DEBUG></DEBUG>\n";
        $reply .= "</USER>\n";
        print $reply;
        return $reply;
    }
}
?>
in a script would cause PHP to tell me I'm trying to redefine udberror_senderrorreply, which does exist in a different file, but one that is not always included. I mean, is there some stupid gotcha in the way function_exists works?

Please do me a favor and ignore anything weird going on in the actual function I pasted up there thanks.
Is it in a class?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


epswing posted:

I've got a block of PHP that needs to avoid concurrent execution. I've thought about writing a small library which uses flock to LOCK_EX a lockfile, and then LOCK_UN when done, so other threads* will wait to acquire their LOCK_EX on said lockfile. It seems PHP will indeed block on a call to flock with a LOCK_EX parameter.

http://ca3.php.net/manual/en/function.flock.php
http://www.tuxradar.com/practicalphp/8/11/0

Is this a Bad Idea (tm)?

That looks like it would work for what you need to do. For the one time I needed to prevent concurrency issues, I just used a PID file since it would catch the new stuff anyways.

epswing
Nov 4, 2003

Soiled Meat

duz posted:

That looks like it would work for what you need to do. For the one time I needed to prevent concurrency issues, I just used a PID file since it would catch the new stuff anyways.

Any PID file in particular, and if so, why?
What do you mean by "the new stuff"?

Munkeymon
Aug 14, 2003

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



supster posted:

Is it in a class?

Nope, but I figured out that, in one case, the other file that defines udbError_sendErrorReply - where there is no guard for function existance - gets included second. It just wasn't clear that was what was going on from the error message. It's all a clusterfuck caused by a stupid decision on someone else's part. Namely, to put the error reporting functionality in a different file then call it without checking to see if it exists and rely on people including both every time.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


epswing posted:

Any PID file in particular, and if so, why?
What do you mean by "the new stuff"?

In this case, an external program was putting data into a database for processing. The PHP script in question was called by cron every 60 seconds which when ran it would check the database for new data. The PID file was made/opened when the script started and was removed when it was done. If the PID file already existed, the script would stop since there was already one running. The script's execution wasn't data bound so it would process whatever data was there.
Maybe not the best way to handle it, but it was a legacy system and had odd restraints.

sonic bed head
Dec 18, 2003

this is naturual, baby!
If I need a user authentication system for something that isn't really critical to having some very strong security on, but I don't want it to be stupidly simple for someone to take advantage of everything, is using PHP's crypt() ok or should I be using something different.

KuruMonkey
Jul 23, 2004
If you just need 'must enter password to access' use .htaccess + .htpasswd (google '.htaccess password protect directory' for how to setup)

Thats the easiest/fastest way to set it up, just have to put all the protected content into a separate directory then setup .htaccess for it.

Remember to put your password file either outside you document root, or if you can't in a dir of its own with a .htaccess of 'deny from all', so it cannot be browsed to.

sonic bed head
Dec 18, 2003

this is naturual, baby!

KuruMonkey posted:

If you just need 'must enter password to access' use .htaccess + .htpasswd (google '.htaccess password protect directory' for how to setup)

Thats the easiest/fastest way to set it up, just have to put all the protected content into a separate directory then setup .htaccess for it.

Remember to put your password file either outside you document root, or if you can't in a dir of its own with a .htaccess of 'deny from all', so it cannot be browsed to.

Thanks for the advice.

I would like a nice form UI though and I also want a session associated with each user. It's not that important in that the information that this login is protecting isn't really critical and no one would care that much if other people got into their account. It's important in that I would like it to look nice and like a regular web application.

Begby
Apr 7, 2005

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

sonic bed head posted:

Thanks for the advice.

I would like a nice form UI though and I also want a session associated with each user. It's not that important in that the information that this login is protecting isn't really critical and no one would care that much if other people got into their account. It's important in that I would like it to look nice and like a regular web application.

Crypt is fine, just make sure you use a different random salt each time.

Stephen
Feb 6, 2004

Stoned
I'm having problems using prepared statements and I'm hoping someone can point out my horrible error.

php:
<?
$qryInsert = '
    INSERT INTO sessions (
        user_id, 
        ip_address, 
        session_start, 
        session_end, 
    ) VALUES (
        :user_id
        , :ip_address
        , :session_start
        , :session_end
    )
';
$aSession = array('user_id' => $userId, 'ip_address' => $ipaddress, 'session_start' => $now, 'session_end' => $expiry);
dbQuery($qryInsert, $aSession);

function dbQuery($stmt, $aParams) {
    try {
        $v = $this->dbo->prepare($stmt); }
        foreach($aParams as $key => $value) { 
            $v->bindParam(':'.$key, $value); 
        }
        $v->execute();
        return $v->fetchAll();
    } catch (PDOException $e) {
        $this->handleError($e);
    }
}
?>
Whenever this is run, the database entry is just the same value copied into each column. (The session_end value)

I can't figure what I'm doing wrong here.

Edit: the connection has already been made etc. so I cut that code out.
Edit again: Nevermind, just figured it out. I should be using bindValue();

Stephen fucked around with this message at 20:34 on Apr 9, 2009

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH

Stephen posted:

Edit again: Nevermind, just figured it out. I should be using bindValue();
It's somewhat important that you understand why this is, so make sure you do. Otherwise you will likely run into other problems down the road.

edit: hint: it has to do with how foreach loops work.

supster fucked around with this message at 21:56 on Apr 9, 2009

Stephen
Feb 6, 2004

Stoned

supster posted:

It's somewhat important that you understand why this is, so make sure you do. Otherwise you will likely run into other problems down the road.
Oh yeah, I definitely know, I just didn't read the PHP documentation as thoroughly as I should have.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
Anyone else excited for 5.3? Anyone test with RC1? Any insight?

KuruMonkey
Jul 23, 2004
Pfffft. It'll be 2 years before thats on any server I work on. The bleeding edge of PHP is just about the top of my list of places not to be.

Ferg
May 6, 2007

Lipstick Apathy

supster posted:

Anyone else excited for 5.3? Anyone test with RC1? Any insight?

I am, though no idea when we'll be seeing it on our production servers after release. It'll be great to have namespace support and late static binding though.

eHacked
Sep 30, 2003

CONGRATULATIONS!!! YOU ARE THE 6,127,436,218TH PERSON TO VIEW THIS USELESS POST. CLICK TO CLAIM YOUR PRIZE!!!
I'm learning OOP from a pretty fantastic book (so far): PHP Object Oriented Solutions by David Powers.

Anyway, he regularly says this "-> operator". Is there a name for '->'?

How the hell do I tell someone what I am talking about in plain English?

KuruMonkey
Jul 23, 2004
member access operator

edit: fnar fnar

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
I need to do a database export from within a PHP application (without using mysqldump) similar to how phpMyAdmin does it. I've searched around a bit and surprisingly did not find anything that did this - does anyone havea any suggestions?

Tad Naff
Jul 8, 2004

I told you you'd be sorry buying an emoticon, but no, you were hung over. Well look at you now. It's not catching on at all!
:backtowork:

supster posted:

I need to do a database export from within a PHP application (without using mysqldump) similar to how phpMyAdmin does it. I've searched around a bit and surprisingly did not find anything that did this - does anyone havea any suggestions?

Well probably since phpmyadmin is written in PHP, you can find out how it does a dump, and just copy/adapt that. On the copy I'm looking at (version 2.9.2), it's in export.php around line 461, the line
code:
} elseif ($export_type == 'database') {
Myself, I'd probably attempt calling mysqldump from PHP first since I'm terribly lazy.

supster
Sep 26, 2003

I'M TOO FUCKING STUPID
TO READ A SIMPLE GRAPH
You can't really use mysqldump through PHP (system() is almost always disabled). I know exactly how phpMyAdmin does it, I was just trying to avoid having to rewrite it all (phpMyAdmin is GPLed and I can't just grab theirs). Anyway I've already resorted to writing an exporter akin to phpMyAdmin's - it wasn't that bad, just a bit tedious.

gibbed
Apr 10, 2006

eHacked posted:

I'm learning OOP from a pretty fantastic book (so far): PHP Object Oriented Solutions by David Powers.

Anyway, he regularly says this "-> operator". Is there a name for '->'?

How the hell do I tell someone what I am talking about in plain English?
-> is officially called the object operator (T_OBJECT_OPERATOR).

eHacked
Sep 30, 2003

CONGRATULATIONS!!! YOU ARE THE 6,127,436,218TH PERSON TO VIEW THIS USELESS POST. CLICK TO CLAIM YOUR PRIZE!!!

KuruMonkey posted:

member access operator

edit: fnar fnar

gibbed posted:

-> is officially called the object operator (T_OBJECT_OPERATOR).

Yeah, object operator sounds like it's the right answer (although I don't have anything to back it up).

I find that putting an actual name to things make it much easier to learn/work with/remember.

Thanks, gibbed.

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

eHacked posted:

I'm learning OOP from a pretty fantastic book (so far): PHP Object Oriented Solutions by David Powers.

Anyway, he regularly says this "-> operator". Is there a name for '->'?

How the hell do I tell someone what I am talking about in plain English?

"The thing that most other languages use a dot for" At least that's what I call it. Was interesting to learn the actual name for it. :)

Fangs404
Dec 20, 2004

I time bomb.
I'm writing a website that has a lot of PHP files that need access to a DB (almost all of them do). Now, in order to avoid having to type something like

php:
<?
$conn = pg_connect('dbname=database user=username password=password');?>
over and over again in every file and have to deal with the inevitable username/password changes (that would have to propagate into every file obviously), I came up with a different solution. I have a file called dbconnect.php and all that's it in it is the above line. All I have to do is a require_once(dbconnect.php) in each page where I want access to the DB.

My questions: Is what I'm doing a security risk (that is, could someone else do a require_once(FULL_PATH/dbconnect.php) and get access to my DB?)? How do you guys deal with this problem?

Thanks!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Fangs404 posted:

My questions: Is what I'm doing a security risk (that is, could someone else do a require_once(FULL_PATH/dbconnect.php) and get access to my DB?)? How do you guys deal with this problem?

Thanks!

If that was a security risk then wouldn't the user also be able "do a pg_connect('dbname=database user=username password=password');". It's the exact same thing as require_once, they are just PHP functions.

Yes, somebody could potentially include that file in a malicious way if they share the same server as you and your host is dumb enough to setup security permissions like that. Hopefully this is unlikely.

As far as a user hitting your website and executing a line of PHP like that, it would not be possible unless you had some poorly written image uploader and some hosed up permissions.

Also, check out using something like PDO for interacting with a database. I typically grab a database connection like:

php:
<?
$database = Database::getConnection();
$query = $database->prepare("select what from huh where this = :that");
$query->bindParam(":that", $that);
?>
I end up calling this all over the place in the app, so I use a singleton pattern to make sure it uses the same connection the whole time and not make a zillion different ones.

fletcher fucked around with this message at 09:51 on Apr 18, 2009

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Fangs404 posted:

I'm writing a website that has a lot of PHP files that need access to a DB (almost all of them do). Now, in order to avoid having to type something like

php:
<?
$conn = pg_connect('dbname=database user=username password=password');?>
over and over again in every file and have to deal with the inevitable username/password changes (that would have to propagate into every file obviously), I came up with a different solution. I have a file called dbconnect.php and all that's it in it is the above line. All I have to do is a require_once(dbconnect.php) in each page where I want access to the DB.

My questions: Is what I'm doing a security risk (that is, could someone else do a require_once(FULL_PATH/dbconnect.php) and get access to my DB?)? How do you guys deal with this problem?

Thanks!


Put anything that stores info you don't want known outside of web root. For example, if your web site is stored at:

/blah/some/path/webRoot/

Then have your files with db passwords and other stuff at:

/blah/some/path/hideMe/

And the web server can't serve the files up at all to people.

Fangs404
Dec 20, 2004

I time bomb.
Awesome, thanks guys. :)

indulgenthipster
Mar 16, 2004
Make that a pour over
edit: Question deleted. Found an easier way to do this

indulgenthipster fucked around with this message at 20:50 on Apr 20, 2009

Adbot
ADBOT LOVES YOU

milieu
Apr 26, 2003
Vizier of Allah
OK, I've got a retard simple php question, I'm sure. But here it goes anyway

I have a huge list of IF->Then type statements in a page template. The reason is I have no idea how many items need to be displayed...could be anywhere from 0 - 200.

So what I'm doing now is putting a huge list like this:

code:
<?php if ($node->field_images[0]['view'] > '') : ?><?php print $node->field_images[0]['view'] ?><?php endif; ?> 
<?php if ($node->field_images[1]['view'] > '') : ?><?php print $node->field_images[1]['view'] ?><?php endif; ?>
...etc. on to 200
Now this is horrible for performance but I don't know any other way to do it. Is there a better way?

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