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
hallik
Aug 15, 2003
I have been looking around, but can't find a good explanation of using multiple $$$

Like why would you have $$var1 or $$$var2? Please explain or point me to a place that explains it.

Or how about how math is done with the e^ stuff. (Sorry if this isn't clear. I was taking some online tests, and have never had to use exponents or that ^, but it wanted me to know)

Adbot
ADBOT LOVES YOU

Inquisitus
Aug 4, 2006

I have a large barge with a radio antenna on it.

hallik posted:

I have been looking around, but can't find a good explanation of using multiple $$$

Like why would you have $$var1 or $$$var2? Please explain or point me to a place that explains it.

Basically $$foo takes the value in the variable $foo and uses it as a variable name for accessing the another variable (this is what the first $ is doing). So if $foo had the value of "baz", then PHP would just access $baz.

hallik posted:

Or how about how math is done with the e^ stuff. (Sorry if this isn't clear. I was taking some online tests, and have never had to use exponents or that ^, but it wanted me to know)

Not really sure what's being asked here. Are you asking how exponentials are computed algorithmically?

hallik
Aug 15, 2003

Inquisitus posted:

Basically $$foo takes the value in the variable $foo and uses it as a variable name for accessing the another variable (this is what the first $ is doing). So if $foo had the value of "baz", then PHP would just access $baz.

So $bar = 'yo';
$$bar would also be accessed by $yo.

What would $$$bar be? I saw a reference to $$$bar. Would that also be $yo? Or am I forgetting something?


Inquisitus posted:

Not really sure what's being asked here. Are you asking how exponentials are computed algorithmically?

Yeah, I guess this is what I am asking for. I just remember seeing a question with that junk in it, and I was like huzzaah? I haven't seen that since h.s. alegbra, and haven't needed it since. It was a test to check my skills in the language, which weren't good, but I was thrown off by the exponent stuff. I don't even remember how they work. Isn't that why we have computars? I know it's good to know, but I just don't remember them, or how they are applied in PHP.

hallik fucked around with this message at 06:38 on May 11, 2008

Atom
Apr 6, 2005

by Y Kant Ozma Post

Anveo posted:

It is stored in memory, but you have the overhead of TCP to access it, plus network latency if the server you are accessing isn't the local one.

Assuming you are reading off a local disk, access is pretty much just doing a file read, plus as a bonus is might also be cached by the OS buffers if it is frequently accessed. (Another option is a ramdisk, which is really fast, but I mention an easier option down a few sentences).

IMHO, the only time you should be using memcached is when you have multiple webservers and want a 'single' distributed cache accessed by all of them.

Bah, poo poo like this is why I prefer to use FastCGI+C. There is no reason for something like memcache to run slower than a flat file read. At least if you roll your own poo poo you know exactly how fast it's gonna be.

hallik posted:

So $bar = 'yo';
$$bar would also be accessed by $yo.

What would $$$bar be? I saw a reference to $$$bar. Would that also be $yo? Or am I forgetting something?


Yeah, I guess this is what I am asking for. I just remember seeing a question with that junk in it, and I was like huzzaah? I haven't seen that since h.s. alegbra, and haven't needed it since. It was a test to check my skills in the language, which weren't good, but I was thrown off by the exponent stuff. I don't even remember how they work. Isn't that why we have computars? I know it's good to know, but I just don't remember them, or how they are applied in PHP.

if $bar = "lol"
and $lol = "wut"
then $$$bar == $wut

Atom
Apr 6, 2005

by Y Kant Ozma Post
Quote != edit.

Inquisitus
Aug 4, 2006

I have a large barge with a radio antenna on it.

hallik posted:

Yeah, I guess this is what I am asking for. I just remember seeing a question with that junk in it, and I was like huzzaah? I haven't seen that since h.s. alegbra, and haven't needed it since. It was a test to check my skills in the language, which weren't good, but I was thrown off by the exponent stuff. I don't even remember how they work. Isn't that why we have computars? I know it's good to know, but I just don't remember them, or how they are applied in PHP.

You really don't need to know how exponentials are actually computed at the algorithmic level. I don't know myself, but I imagine it'd be something to do with Taylor expansions.

If you just want to get the result of an exponential with base e, then use PHP's exp function.

Inquisitus fucked around with this message at 10:05 on May 11, 2008

hallik
Aug 15, 2003

Inquisitus posted:

You really don't need to know how exponentials are actually computed at the algorithmic level. I don't know myself, but I imagine it'd be something to do with Taylor expansions.

If you just want to get the result of an exponential with base e, then use PHP's exp function.

Good to know. This was one of those questions that was like "what is the result? a) 13, b) 14, c) 15 etc...

I will just read up on exponents and what the ^ function does. It's really stupid I was actually asked that I thought. And your response confirms, at least in my mind, it was a dumb question to ask on a php 'prove it' test...

hey mom its 420
May 12, 2007

Dominoes posted:

I just learned PHP, and set up a simple script to email me submitted form data. I have a small problem that I"m hoping someone can help me with.

Here's the website with form.

Here's the PHP file:
code:
<html>
<body>
<?php

$to = "contact@equilibriumpomade.com";
$subject = "Question/Comment";
$name = $_REQUEST["name"];
$message = $_REQUEST["question"];
$from = $_REQUEST["email"];
$headers = "From: $from";

mail($to,$subject,"Name: $name
Message: $message",$headers);
echo "Message Sent!";
?>
</body>
</html>
The emails are received, but the person using the form is redirected to the php page with the printed text. How do I direct the user to an HTML page instead? I don't want them to go to the php page, I just want to use the script to process the form data.
One word of advice: You are receiving data from the user and putting it directly into emails that you send. Much like putting in data from the user into the database without escaping it, you're leaving yourself open for an injection attack here. Only here it's a header injection attack, meaning that anyone can use your email form to send spam from your server to anyone, not just you. This is okay if you're just learning how emails in PHP generally work, but if you're going to be putting it up for use, it's a good idea to filter it.
I just asked about PHP mailing libraries one page ago and at the end I found the Swift library, which filters the data for you. I recommend it, works nicely and I got it set up and working in a matter of minutes.

hey mom its 420
May 12, 2007

what the hell

I'm trying to have a user upload an image and then I resize it with gd and save it. I'm using a mix of functions that I got from php.net and some other place. Thing is, it works for very small images, but if I try a bigger image (like 250k), I get this error message
code:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 7500 bytes) in 
F:\AppServ\www\klancar2\funcs.php on line 61
How the hell does it exhaust 16 megs of allowed memory size when trying to allocate 7500 bytes?

I could post the functions I use to convert the images but they're pretty long, basically it's just finding out what the type of the image is with a lot of case structures, then it calculates the new width and height and then it calls imagecopyresampled and then it saves it.

hey mom its 420 fucked around with this message at 17:32 on May 11, 2008

DaTroof
Nov 16, 2000

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

Bonus posted:

what the hell

I'm trying to have a user upload an image and then I resize it with gd and save it. I'm using a mix of functions that I got from php.net and some other place. Thing is, it works for very small images, but if I try a bigger image (like 250k), I get this error message
code:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 7500 bytes) in 
F:\AppServ\www\klancar2\funcs.php on line 61
How the hell does it exhaust 16 megs of allowed memory size when trying to allocate 7500 bytes?

The error message is a little misleading. It means that PHP was already using its maximum memory size and tried to allocate an additional 7500 bytes over the limit.

Anveo
Mar 23, 2002

Atom posted:

Bah, poo poo like this is why I prefer to use FastCGI+C. There is no reason for something like memcache to run slower than a flat file read. At least if you roll your own poo poo you know exactly how fast it's gonna be.

Memcached is pretty efficient C, you just need to remember it is by design a network distributed memory cache and not a local memory cache, so it makes perfect sense it will be slower than a local file read. It is still fast and awesome.

No need to rewrite things yourself, if you mistook it for a local memory cache just use one that already exists and suggested before such as shm functions or apc.

Zorilla
Mar 23, 2005

GOING APE SPIT

Bonus posted:

code:
Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 7500 bytes) in 
F:\AppServ\www\klancar2\funcs.php on line 61
How the hell does it exhaust 16 megs of allowed memory size when trying to allocate 7500 bytes?

JPEG images take up much more space when loaded into RAM because they get decompressed. Plus, I bet you're making multiple copies of it in RAM. Is the 7.5k image fairly large in dimension too?

I don't know if it works, but when you're done with a certain instance of an image, you might try using unset() on the variable containing that instance to free up some RAM.

Or you could do what I did for my image uploader and raise the php.ini memory limit to 24 MB using ini_set().

Zorilla fucked around with this message at 01:34 on May 12, 2008

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


That's why you always use image_destroy when you're done with that copy of the image.

Dominoes
Sep 20, 2007

Zorilla posted:

Probably something like this:

php:
<?php
isset($_POST["submit"])
    or header("Location:index.html");

$to "contact@equilibriumpomade.com";
$subject "Question/Comment";
$name $_REQUEST["name"];
$message $_REQUEST["question"];
$from $_REQUEST["email"];
$headers "From: " $from;

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
    "http://www.w3.org/TR/html4/loose.dtd">

<head>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
    <title>Equilibrium Natural Pomade</title>
    <meta http-equiv="refresh" content="3;url=index.html">
</head>

<body>
<?php
if (mail($to,$subject,"Name: " $name "\r\nMessage: " $message,$headers)) {
?>
Email sent successfully
<?php
} else {
?>
Error: the message could not be sent
<?php
}
?>
</body>
</html>

You'll still have to load this page and then it will display a message, then it will go back to wherever you want it to go 3 seconds later. If you want to send the email without ever leaving the page you're coming from, you'll probably need to use AJAX.

Thanks, that worked! It actually doesn't wait 3 seconds and redirect to the "url=whatever", it immediately goes to the home page. That's more than adequate for now. :)

noonches posted:

At the very top of the page put ob_start() and after the echo put:
php:
<?
ob_end_clean()
header("Location: thank-page.php");
die();
?>

It gives me PHP errors. Thanks though. :)

Bonus posted:

One word of advice: You are receiving data from the user and putting it directly into emails that you send. Much like putting in data from the user into the database without escaping it, you're leaving yourself open for an injection attack here. Only here it's a header injection attack, meaning that anyone can use your email form to send spam from your server to anyone, not just you. This is okay if you're just learning how emails in PHP generally work, but if you're going to be putting it up for use, it's a good idea to filter it.
I just asked about PHP mailing libraries one page ago and at the end I found the Swift library, which filters the data for you. I recommend it, works nicely and I got it set up and working in a matter of minutes.

I'll try and experiment with that later, but I hadn't touched PHP before yesterday, so I'm trying to keep it simple first.

Zorilla
Mar 23, 2005

GOING APE SPIT

Dominoes posted:

Thanks, that worked! It actually doesn't wait 3 seconds and redirect to the "url=whatever", it immediately goes to the home page. That's more than adequate for now. :)
Oops, that's because it loads the refresh tag, then takes a couple seconds to do the mail send work right in the middle of a page load. That 3-second wait is probably already eaten up by the time the page finishes. The page would probably load more cleanly if mail() happens before the first HTML appears.

hey mom its 420
May 12, 2007

Thanks guys, I managed to solve it by adding around more calls to free up resources and such.
Also, here's something that drove me up the wall today. IE uploads jpg files as image/pjpeg while other browsers do it as image/jpg. I spent at least an hour and then some running around trying to find out why images uploaded with IE don't work, because I was turning the second part of the MIME type directly into the extension. Ugh!

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Bonus posted:

because I was turning the second part of the MIME type directly into the extension. Ugh!

That can be changed by the user so never do that, use content sniffing.

other people
Jun 27, 2004
Associate Christ
I just hit a snag.

When you submit a form, if a checkbox is not checked the resulting POST data doesn't have an element name with no value, it doesn't submit anything at all relating to the check box.

The script that receives the POST data does not know the name of the checkboxes. This form does a db update, and if the boxes are not checked the resulting fields are never set to zero, they just remain at their old value since they don't appear in the update.

Toanek
Feb 21, 2008

It's a fiesta of personal triumph!

Kaluza-Klein posted:

I just hit a snag.

When you submit a form, if a checkbox is not checked the resulting POST data doesn't have an element name with no value, it doesn't submit anything at all relating to the check box.

The script that receives the POST data does not know the name of the checkboxes. This form does a db update, and if the boxes are not checked the resulting fields are never set to zero, they just remain at their old value since they don't appear in the update.

Not too sure how you're handling the post data but you could just do something like $checked_bool = isset( $_POST['checkbox_name'] ), right?

e: I misread that part about not knowing the post names, whoops. :downs:

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice
[edit] never mind. Didn't see the crazy "my script doesn't know what I am sending it" caveat.

Lumpy fucked around with this message at 02:58 on May 12, 2008

Anveo
Mar 23, 2002

Kaluza-Klein posted:

I just hit a snag.

When you submit a form, if a checkbox is not checked the resulting POST data doesn't have an element name with no value, it doesn't submit anything at all relating to the check box.

The script that receives the POST data does not know the name of the checkboxes. This form does a db update, and if the boxes are not checked the resulting fields are never set to zero, they just remain at their old value since they don't appear in the update.

Do you know the names of all the table fields? If so just update them all to zero, and then update them again with the form data.

other people
Jun 27, 2004
Associate Christ

Anveo posted:

Do you know the names of all the table fields? If so just update them all to zero, and then update them again with the form data.

Yes, I can get those. I will have to query for the table field names, and if I don't have a $_POST to match it then it gets set to 0.

A bit off topic, but it seems like when submitting forms values all form elements should be sent, even if they are null or blank or disabled etc. There are probably good reasons why they are not, but I have yet to notice them.

Atom
Apr 6, 2005

by Y Kant Ozma Post

Anveo posted:

Memcached is pretty efficient C, you just need to remember it is by design a network distributed memory cache and not a local memory cache, so it makes perfect sense it will be slower than a local file read. It is still fast and awesome.

No need to rewrite things yourself, if you mistook it for a local memory cache just use one that already exists and suggested before such as shm functions or apc.

Well yeah but still in the degenerate case of one machine in the distribution, it should just degrade into normal memory caching.

Of course, it doesn't matter anyways because we're piddling over what probably winds up being a small fraction of the execution time anyways.

:3:

Evil Angry Cat
Nov 20, 2004

Kaluza-Klein posted:

Yes, I can get those. I will have to query for the table field names, and if I don't have a $_POST to match it then it gets set to 0.

A bit off topic, but it seems like when submitting forms values all form elements should be sent, even if they are null or blank or disabled etc. There are probably good reasons why they are not, but I have yet to notice them.

Can you not do

php:
<?php
//ice cream flavour chooser
if (isset($_POST['checkboxes']))
{
    $to_check unserialize($_POST['checkboxes']);
    foreach ($to_check as $flavour)
    {
        if (isset($_POST[$flavour]))
        {
            //doWhateverYouNeedToDo if checkbox is selected
        }
        else
        {
            //checkbox wasnt selected
        }
    }
}

$checkboxes = array ("Chocolate""Vanilla""Pastis");

echo '<form action="flavour_picker.php" method="POST" />';

foreach ($checkboxes as $checkbox)
{
    echo $checkbox.'<input type="checkbox" name="'.$checkbox.'" />';
}

echo '<input type="hidden" name="checkboxes" value="'.serialize($checkboxes).'" />';
echo '<input type="submit" name="submit" value="Submit" />';
echo '</form>';
?>

raej
Sep 25, 2003

"Being drunk is the worst feeling of all. Except for all those other feelings."
I can't seem to find a good tutorial on how to enter a birth date in a user registration form. I'm not sure how I should store it in my database, or how to parse it to that format.

Should I go with one input box and do a lot of parsing to figure out which of the inputs are numbers, and which are slashes or dashes, or do some sort of 3 box setup and put it all together?

Evil Angry Cat
Nov 20, 2004

raej posted:

I can't seem to find a good tutorial on how to enter a birth date in a user registration form. I'm not sure how I should store it in my database, or how to parse it to that format.

Should I go with one input box and do a lot of parsing to figure out which of the inputs are numbers, and which are slashes or dashes, or do some sort of 3 box setup and put it all together?

I generally do three input boxes that already have MM DD YYYY in them as it takes less time to write parsing from different possible entry types to a timestamp. Use mktime() to change the input(s) you get from the form into a timestamp and then store it as a TIME (or maybe DATETIME?) in your database.

raej
Sep 25, 2003

"Being drunk is the worst feeling of all. Except for all those other feelings."

Evil Angry Cat posted:

I generally do three input boxes that already have MM DD YYYY in them as it takes less time to write parsing from different possible entry types to a timestamp. Use mktime() to change the input(s) you get from the form into a timestamp and then store it as a TIME (or maybe DATETIME?) in your database.

Oooh, that seems a lot easier than I was thinking. Thanks!

Munkeymon
Aug 14, 2003

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



I have a question about configuration. When I set up PHP on my local machine I have tried giving it Lat/Long and a regular timezone (and both), but it keeps translating that into America/Chicago. That's the correct time and whatnot, but not an actual time zone that people recognise. They assume its a location (go loving figure). Can I get it to use real time zones instead of the made up crap that the open source community seems to be fond of without messing around with the time zone database?

raej posted:

I can't seem to find a good tutorial on how to enter a birth date in a user registration form. I'm not sure how I should store it in my database, or how to parse it to that format.

Should I go with one input box and do a lot of parsing to figure out which of the inputs are numbers, and which are slashes or dashes, or do some sort of 3 box setup and put it all together?

Do either a 3 box setup with dropdowns and validate it with checkdate or you could let them type it in and pass it to strtotime (or a similar function)*, which is going to be much better at parsing dates than anything you could write by yourself in a reasonable amount of time. I don't have any advice about database storage, but I know MySQL has a DateTime type or something similar.

*There could be a security concern, but if there is, I'm shure if you give it a second someone else will probably mention that and call me an idiot for suggesting passing user input to strtotime.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Evil Angry Cat posted:

I generally do three input boxes that already have MM DD YYYY in them as it takes less time to write parsing from different possible entry types to a timestamp. Use mktime() to change the input(s) you get from the form into a timestamp and then store it as a TIME (or maybe DATETIME?) in your database.

Why store a date as a TIME in your database? Why not store it as, I don't know, a DATE?

Evil Angry Cat
Nov 20, 2004

duz posted:

Why store a date as a TIME in your database? Why not store it as, I don't know, a DATE?

I've never really heard of a datestamp before have you?

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


Evil Angry Cat posted:

I've never really heard of a datestamp before have you?

No, which is why I said DATE.

Evil Angry Cat
Nov 20, 2004

duz posted:

No, which is why I said DATE.



The reason I store as a timestamp rather than using MySQLs inbuilt DATE type is because I personally find it easier to manipulate with php. Apologies, it's been so long since I used DATE that I'd forgotten it wasn't just another timestamp type.

hey mom its 420
May 12, 2007

It's easy to convert from a MySQL DATETIME field to a timestamp by using the UNIX_TIMESTAMP MySQL function or to convert from a timestamp to a DATETIME field by using the FROM_UNIXTIME MySQL function, so in the end it largely depends on preference.

cLin
Apr 21, 2003
lat3nt.net
What is the best approach to stop sql injections? Should I just use mysql_real_escape_string before inputting the variables into mysql? Is there anything else I should do?

Toanek
Feb 21, 2008

It's a fiesta of personal triumph!

cLin posted:

What is the best approach to stop sql injections? Should I just use mysql_real_escape_string before inputting the variables into mysql? Is there anything else I should do?

mysql_real_escape_string is about all you need. For simplicity's sake you can also just use intval() if the value's going to be an integer.

duz
Jul 11, 2005

Come on Ilhan, lets go bag us a shitpost


cLin posted:

What is the best approach to stop sql injections? Should I just use mysql_real_escape_string before inputting the variables into mysql? Is there anything else I should do?

Use an abstraction layer like MDB2 and use prepared statements. It'll handle type checking and everything for you.

hey mom its 420
May 12, 2007

I use ADOdb, it's pretty cool, especially parametrization, where you do $db->execute("SELECT * FROM foo WHERE foo.bar = ?", array("baz"))

Grigori Rasputin
Aug 21, 2000
WE DON'T NEED ROME TELLING US WHAT TO DO
Anyone know of a good resource for how to implement a user management system in a *AMP environment? I want to make one that's pretty basic, should be able to:

self-register
login
add data
edit data
maintain a session (don't know much about this end...)

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.

Thanks!

cLin
Apr 21, 2003
lat3nt.net

duz posted:

Use an abstraction layer like MDB2 and use prepared statements. It'll handle type checking and everything for you.

I'm guessing it's similar to the post below yours where you just execute a sql query using one of their methods, but are either of these small in size? I don't want to make a program even slower by including a big file.

Adbot
ADBOT LOVES YOU

Begby
Apr 7, 2005

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

cLin posted:

I'm guessing it's similar to the post below yours where you just execute a sql query using one of their methods, but are either of these small in size? I don't want to make a program even slower by including a big file.

You can use PDO too

https://www.php.net/PDO

code:
$pdo = new PDO($dsn, $login, $password);

$stmt = $pdo->prepare("SELECT * FROM people WHERE name=:name");
$stmt->BindParam(":name", "fred");
$stmt->execute();
This will let you do prepared statements securely and it is a compiled module so you aren't going to have to include any big libraries or anything.

Its not bundled with all distributions of PHP 5 though, so YMMV.

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