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
duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Grigori Rasputin posted:

As far as password storage goes, is it best just to use PHP encryption methods to encrypt/store and decrypt/authenticate, or is there more the database should do? I dug around to see if there was a way to auto-encrypt a column in MySQL but didn't see much.

No, hash the password with a salt and store that and the salt. No need to use encryption.

Adbot
ADBOT LOVES YOU

Tron Paul
Dec 5, 2006

I've got a question that can probably be answered pretty easily. I'm more of a designer and Flash/Actionscript programmer, so I don't really have much knowledge about PHP other than obvious coding conventions.

Anyway, I'm trying to get a form to post to a CSV file for Excel output. After a few hours, I got everything to work right, except for some reason the data isn't added to the file, it just overwrites what was previously there.


It seems to me I'm just not putting in a simple argument somewhere, but any help is definitely appreciated.

DaTroof
Nov 16, 2000

CC LIMERICK CONTEST GRAND CHAMPION
There once was a poster named Troof
Who was getting quite long in the toof

usingpond posted:

I've got a question that can probably be answered pretty easily. I'm more of a designer and Flash/Actionscript programmer, so I don't really have much knowledge about PHP other than obvious coding conventions.

Anyway, I'm trying to get a form to post to a CSV file for Excel output. After a few hours, I got everything to work right, except for some reason the data isn't added to the file, it just overwrites what was previously there.


It seems to me I'm just not putting in a simple argument somewhere, but any help is definitely appreciated.

You're probably opening a file handler for writing instead of appending, e.g.:

$f = fopen('file.txt', 'w');

The 'w' should be 'a' to append instead of overwrite.

$f = fopen('file.txt', 'a');

http://www.php.net/fopen

Tron Paul
Dec 5, 2006

DaTroof posted:

You're probably opening a file handler for writing instead of appending, e.g.:

$f = fopen('file.txt', 'w');

The 'w' should be 'a' to append instead of overwrite.

$f = fopen('file.txt', 'a');

http://www.php.net/fopen

Wow, I knew it was something like that. I was sitting there looking suspiciously at the 'w', but then I thought to myself "Usingpond, please. Like you know what anything means here."

That was it, thanks so much!

fopen('myconfidence.txt', 'a');

EvilBert
Oct 26, 2002

Bleak Gremlin
This is probably more a permissions than a programming question but at least I am loving around with PHP :-)

I am running external programs with the exec command to obtain information about my servers' status via simple webpages instead of having to remote access into it.

It works with smartctl (reads SMART status of drives) and logparser (creates HTML reports of the eventlog).

Now I want to create an HTML report showing the hard drive and case temperatures and some other poo poo with SIW. Whenever I start SIW with the script it idles a bit, I hear a "beep" from the server and thats it. No error, no report is created. When I start it via remote access from the command line it works perfect. But when I start SIW from the command line it actually shows a brief popup on the desktop before it starts working. This can not be switched of. So I assume that this is the point where the start from the script fails as I assume SIW will not be able to access the desktop and just stops.

As my permission setup allows logparser to work there seems to be some problem with the desktop interaction...

Did anybody ever encounter something like this? Can it be solved?

duck monster
Dec 15, 2004

redacted. unnecesarily agro.

other people
Jun 27, 2004
Associate Christ
Why does this only work for the "The " value? If the string starts with "A " or "An " it doesn't catch it.

php:
<?
function format_title($string) {
     $bit_array = array('The ' => ', The', 'A ' => ', A', 'An ' => ', An');

     foreach ($bit_array as $bit => $newbit) {
          if (strpos($string, $bit) === 0) {
               $string = ltrim($string, $bit) . $newbit;
          };
     };
}
?>

Evil Angry Cat
Nov 20, 2004

It's the === 0.

php:
<?
function format_title($string) {
    $bit_array = array('The ' => ', The', 'A ' => ', A', 'An ' => ', An');
    
    foreach ($bit_array as $prev => $new)
    {
        if (strpos($string, $prev)!==FALSE)
        {
            $string = (strpos($string, $prev)==0) ? ltrim($string, $prev) . $new : $string;
        }
    }
    
    return $string;
}
?>
This should work properly

other people
Jun 27, 2004
Associate Christ

Evil Angry Cat posted:

It's the === 0.

php:
<?
function format_title($string) {
    $bit_array = array('The ' => ', The', 'A ' => ', A', 'An ' => ', An');
    
    foreach ($bit_array as $prev => $new)
    {
        if (strpos($string, $prev)!==FALSE)
        {
            $string = (strpos($string, $prev)==0) ? ltrim($string, $prev) . $new : $string;
        }
    }
    
    return $string;
}
?>
This should work properly

What is $string = (strpos($string, $prev)==0) ? ltrim($string, $prev) . $new : $string; doing exactly? The if statement is making sure strpos() doesn't return false, and then in that line you are checking to make sure it returns the zero position?

I've just never seen syntax like that. And the bit at the end there: ltrim() . $new : $string;? What is the colon doing?

Thank you for helping, I am just simple :(.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Kaluza-Klein posted:

I've just never seen syntax like that. And the bit at the end there: ltrim() . $new : $string;? What is the colon doing?

Thank you for helping, I am just simple :(.

Are you talking about the Ternary Operator?

http://www.php.net/operators.comparison

minato
Jun 7, 2004

cutty cain't hang, say 7-up.
Taco Defender

Kaluza-Klein posted:

Why does this only work for the "The " value? If the string starts with "A " or "An " it doesn't catch it.
It works fine for me. Except the function isn't actually returning the modified string.

other people
Jun 27, 2004
Associate Christ

fletcher posted:

Are you talking about the Ternary Operator?

http://www.php.net/operators.comparison

Yes I was. I have never come across it before. That could make for some more concise code. Thank you.

minato posted:

It works fine for me. Except the function isn't actually returning the modified string.

I typed that in the post by hand and forgot that part, the return $string; statement is in the real function and it still doesn't work for me.



And I am a complete loving moron! This function was replacing an old function. It is called from two different places and I forgot to change the function name in both places. So in the case I was checking it was calling the old function that just changes the "The " in a string.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Is it better to let the database (mysql) handle timezone conversions by setting the session timezone than to do it in PHP?

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO
I have a question that might be a little involved, but maybe someone can point me in the right direction.

Basically, I want to create a system that will automatically download an email (whenever I receive it) to the php webserver, parse it for relevant data and toss it in the db.

Basically, I have no idea how to connect to a mail server or locate the email whatsoever. I can think of a couple clunky ways to do it, just about all of which involve manual intervention. I'm looking to automate as much as possible.

Thanks!

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

Grigori Rasputin posted:

I have a question that might be a little involved, but maybe someone can point me in the right direction.

Basically, I want to create a system that will automatically download an email (whenever I receive it) to the php webserver, parse it for relevant data and toss it in the db.

Basically, I have no idea how to connect to a mail server or locate the email whatsoever. I can think of a couple clunky ways to do it, just about all of which involve manual intervention. I'm looking to automate as much as possible.

Thanks!

Use this to get the mail
http://us2.php.net/imap

Use these functions to find information in the email
http://us2.php.net/manual/en/ref.strings.php

Use this to stuff it into a database
http://us2.php.net/pdo

Use a cron job to run it every x minutes

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO
You're awesome, that's exactly the info I need.

bt_escm
Jan 10, 2001

Grigori Rasputin posted:

I have a question that might be a little involved, but maybe someone can point me in the right direction.

Basically, I want to create a system that will automatically download an email (whenever I receive it) to the php webserver, parse it for relevant data and toss it in the db.

Basically, I have no idea how to connect to a mail server or locate the email whatsoever. I can think of a couple clunky ways to do it, just about all of which involve manual intervention. I'm looking to automate as much as possible.

Thanks!

Zend_Mail has a really nice way of doing this http://framework.zend.com/manual/en/zend.mail.read.html

A Flaming Chicken
Feb 4, 2007

fletcher posted:

Is it better to let the database (mysql) handle timezone conversions by setting the session timezone than to do it in PHP?

I believe there is some stupidity in PHP using timezones if you are naive about it. You must use a putenv statement to only do it in the script you are currently running, else it affects all threads executing.

MySQL only supports a single timezone - UTC?

nbv4
Aug 21, 2002

by Duchess Gummybuns
I have a PHP array with each item being an airport identifier. "KBOS", "KTEB", "KMIA", "MMIO", etc. These are wordwide airports, not just US airports. I want to get the longitude/latitude coordinates of each item. It's a long shot, but is there a tool that already does such a thing? I can probably swing together a function that crawls some other page for the info, but I don't want to waste my time if something already exists.

Once I get the longitude/latitude coordinates, I want to map them all on a map with lines connecting some of them. Whats a good tool for doing this? Would the Google Maps API be the only way to achieve this? I'd rather have it be a static image that can be saved.

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO
Another day, another PHP question. Is there a way to hide URL arguments like "index.html?pageid=123&type=x" from the user? I figured it would be configurable in php.ini but I haven't stumbled upon anything yet.

Also, is using URL arguments like this the preferred way to communicate between pages? It's very easy to do, but I wasn't sure if there was a better way.

Zorilla
Mar 23, 2005

GOING APE SPIT

Grigori Rasputin posted:

Another day, another PHP question. Is there a way to hide URL arguments like "index.html?pageid=123&type=x" from the user? I figured it would be configurable in php.ini but I haven't stumbled upon anything yet.

Also, is using URL arguments like this the preferred way to communicate between pages? It's very easy to do, but I wasn't sure if there was a better way.

- Use postdata ($_POST) instead of submitting data through the URL and/or use mod_rewrite to clean up the URL a bit (i.e. site.com/index.html?pageid=123&type=x becomes site.com/123/x). Things like page numbers should probably stay in the URL, but minor stuff like "sort by" settings for viewing data in a table should be sent as postdata and retained as a cookie.

- Query strings (URL arguments) are preferred if you want a particular page to be linkable from outside. Use postdata for form submissions though.

Zorilla fucked around with this message at 23:21 on May 21, 2008

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


You can also use sessions if you're not getting the data from that page.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

A Flaming Chicken posted:

MySQL only supports a single timezone - UTC?

Can't you do a SET time_zone = +10:00?

Begby
Apr 7, 2005

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

Grigori Rasputin posted:

Another day, another PHP question. Is there a way to hide URL arguments like "index.html?pageid=123&type=x" from the user? I figured it would be configurable in php.ini but I haven't stumbled upon anything yet.

Also, is using URL arguments like this the preferred way to communicate between pages? It's very easy to do, but I wasn't sure if there was a better way.

Keep in mind that one thing that is handy about having those arguments on the URL line is that it makes it so you can store the page in a bookmark. If you use sessions or post that won't work. Also, if you use post, then clicking on the back button in your browser can generate warnings.

You can also use a framework that will let you do more human readable arguments like

http://www.mysite.com/products/page/123/type/x

Most users though really don't care about what is in the URL. Go to some major sites and look up there and see all the incomprehensible garbage that ends up there.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Do you guys pass in a database connection in to a static function or do you get the database connection from within the static function?

MonkeyMaker
May 22, 2006

What's your poison, sir?
I'm trying to use the PHP-native result from the Flickr API and I have everything working well enough except when I try to display results from more than one photoset on the same page. It returns the same images for all photoset calls. Any ideas?

Here's my code on pastie: http://pastie.caboo.se/202515

Treytor
Feb 8, 2003

Enjoy, uh... refreshing time!
Okay I've got a question. I have a form that I want filled out, but I want a hidden field to be randomized.

code:
<input name="random_file" type="hidden" value="cn1" />
I want the value to be a random selection from an array, cn1 through cn30.

Any thoughts on how this could be accomplished?

edit: I think I figured it out!

code:
<input name="random_file" type="hidden" value="<?php echo "cn".rand(1,31); ?>" />

Treytor fucked around with this message at 08:49 on May 24, 2008

Zorilla
Mar 23, 2005

GOING APE SPIT
Either that or when displaying the form, generate a random md5 for the hidden field, then store it in a session variable that gets checked when the form gets posted.

Xenos
Jun 17, 2005

Grigori Rasputin posted:

Also, is using URL arguments like this the preferred way to communicate between pages? It's very easy to do, but I wasn't sure if there was a better way.

This is really a broad question.

Data sharing between pages can be done in a lot of different ways, but it really depends on what you're trying to do. If you're trying to make a form of sign-on system, the most common way to go about that would be to store some info in a session file server-side (PHP does this via session_start and $_SESSION), then have your pages check for the given session info that you set when the user logged in.

When it comes down to accessing info, such as a page to edit a user's profile page, using the query string is the way to go. It would be something like profile.php?profile=blah, which would contain a form that submits to itself via POST. When it comes down to it, I'm just arguing for normal usage of GET and POST -- GET is used to access resources non-destructively, POST is used to modify them. All I really try to do is use the HTTP methods for what they were designed to do in the basic sense, but I'm straying off topic so I'll stop there.

I hope this helps you out :)

MrEnigma
Aug 30, 2004

Moo!
Is there any extension/package that will do form creation, and then auto detect if the form values were tampered with (ie with firebug or rewriting the page).

I know .NET has something either built in or available, but was wondering besides rolling your own, if there was anything people use with PHP.

I know there is Filter, but that only validates inputs.

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO

Xenos posted:

This is really a broad question.

Data sharing between pages can be done in a lot of different ways, but it really depends on what you're trying to do. If you're trying to make a form of sign-on system, the most common way to go about that would be to store some info in a session file server-side (PHP does this via session_start and $_SESSION), then have your pages check for the given session info that you set when the user logged in.

When it comes down to accessing info, such as a page to edit a user's profile page, using the query string is the way to go. It would be something like profile.php?profile=blah, which would contain a form that submits to itself via POST. When it comes down to it, I'm just arguing for normal usage of GET and POST -- GET is used to access resources non-destructively, POST is used to modify them. All I really try to do is use the HTTP methods for what they were designed to do in the basic sense, but I'm straying off topic so I'll stop there.

I hope this helps you out :)

Basically, I'm working on a site that does all the basic things to sort of teach myself PHP and get familiar with the world of web programming. Right now I'm working on a user authentication/session system and am trying to find the best way to go about doing it. GET/POST are really straightforward to use, I've been using them to pass variables between pages and it's all working good - I figured this is preferred, I'm just focused on learning best practices since I'm still learning and don't want to get in the habit of using hacks.

As far as the user authentication/session system, my intuitive approach seems a little clunky. Basically, I have a header.php and footer.php included to fill out the bulk of the base HTML between pages. I have a check inside header.php that checks to see if a user has logged in and if there is a valid session. If there is, the user proceeds as normal, if not then the user is redirected to my login page and told they need to login to perform that action. I know that there are probably a million ways to do this, but my solution doesn't seem quite optimal. What kind of strategies have other people adopted for user sessions after authenticating?

Quick bonus question: how do you set a session to expire after x minutes of inactivity?

bt_escm
Jan 10, 2001

MrEnigma posted:

Is there any extension/package that will do form creation, and then auto detect if the form values were tampered with (ie with firebug or rewriting the page).

I know .NET has something either built in or available, but was wondering besides rolling your own, if there was anything people use with PHP.

I know there is Filter, but that only validates inputs.

There's Zend_Form that does both form creation and validation. I'm pretty sure it won't autodetect tampering. There's also something in the PEAR libraries, but it doesn't do tampering either. I'm pretty sure you're going to have to write your own for that.

bt_escm
Jan 10, 2001

fletcher posted:

Do you guys pass in a database connection in to a static function or do you get the database connection from within the static function?

I get the database connection from within the static function.

bt_escm
Jan 10, 2001

Grigori Rasputin posted:


As far as the user authentication/session system, my intuitive approach seems a little clunky. Basically, I have a header.php and footer.php included to fill out the bulk of the base HTML between pages. I have a check inside header.php that checks to see if a user has logged in and if there is a valid session. If there is, the user proceeds as normal, if not then the user is redirected to my login page and told they need to login to perform that action. I know that there are probably a million ways to do this, but my solution doesn't seem quite optimal. What kind of strategies have other people adopted for user sessions after authenticating?

Quick bonus question: how do you set a session to expire after x minutes of inactivity?

Your authentication scheme is fine for a basic site. I'm assuming you have a collection of php pages that contain the layout and any processing logic in them. Doing that is fine for a smaller site or a site that doesn't really do anything. If you goal is to try and build something more complicated than a site with bunch of content and a few contact forms, then I recommend looking into a framework.

To force the sessions to time out set session.gc_maxlifetime in your php.ini to however many seconds you want before the session file is erased. If you are on a shared host then you may need to use ini_set('session.gc_maxlifetime',#seconds) before you call session start or set it in a .htaccess file for your whole site.

bt_escm
Jan 10, 2001

MonkeyMaker posted:

I'm trying to use the PHP-native result from the Flickr API and I have everything working well enough except when I try to display results from more than one photoset on the same page. It returns the same images for all photoset calls. Any ideas?

Here's my code on pastie: http://pastie.caboo.se/202515

change line 60 to
php:
<?
$rsp = hitFlickr('',$photoset);
?>
Also you can change lines 17 - 23 to just this
php:
<?
$url = "http://api.flickr.com/services/rest/?".http_build_query($params);
?>

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


bt_escm posted:

To force the sessions to time out set session.gc_maxlifetime in your php.ini to however many seconds you want before the session file is erased. If you are on a shared host then you may need to use ini_set('session.gc_maxlifetime',#seconds) before you call session start or set it in a .htaccess file for your whole site.

The default is 24 minutes of inactivity.

bt_escm
Jan 10, 2001

nbv4 posted:

I have a PHP array with each item being an airport identifier. "KBOS", "KTEB", "KMIA", "MMIO", etc. These are wordwide airports, not just US airports. I want to get the longitude/latitude coordinates of each item. It's a long shot, but is there a tool that already does such a thing? I can probably swing together a function that crawls some other page for the info, but I don't want to waste my time if something already exists.

Once I get the longitude/latitude coordinates, I want to map them all on a map with lines connecting some of them. Whats a good tool for doing this? Would the Google Maps API be the only way to achieve this? I'd rather have it be a static image that can be saved.

I found this http://www.webservicex.com/airport.asmx and I was able to get a couple of coordinates for a few airports.

I think google maps would be ideal for this.

MonkeyMaker
May 22, 2006

What's your poison, sir?

bt_escm posted:

change line 60 to
php:
<?
$rsp = hitFlickr('',$photoset);
?>
Also you can change lines 17 - 23 to just this
php:
<?
$url = "http://api.flickr.com/services/rest/?".http_build_query($params);
?>

I can't say thank you enough. I don't write PHP often enough any more and I seem to miss completely obvious things. Thanks a lot.

ilikechapstick
Jun 17, 2007

very, very notorious.
Quick question about PHP, I have used it only a couple times and have very limited PHP knowledge.

I have a phone number (of type string) in a database formatted as xxxxxxxxx.

I would like it to be printed as xxx-xxx-xxxx.

My job is to design the webpages, however this is pissing me off from a design standpoint so I need to figure it out. I can only assume I would need some kind of loop, but help me please!

Adbot
ADBOT LOVES YOU

magicalblender
Sep 29, 2007
How about something like this:
code:
$hyphenatedstring = substr($string,0,3) . "-" . substr($string, 3,3) . "-" . substr($string,6,4);
edit: whoops, forgot my $s and ;s

magicalblender fucked around with this message at 23:47 on May 25, 2008

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