/    Sign up×
Community /Pin to ProfileBookmark

A general question about php

I am trying to learn php and it sometimes seems like a daunting task. What I am trying to accomplish is to setup a simple user login the users can securely upload files to a server. If the user does not have a login I want to be able to allow them to register, in that they fill out a form, when they submit the form the information from the form is sent via e-mail to several different people, those people then create an account for the user who submitted the form and reply to the user with a user name, password, and a confirmation code of somekind. When the user has received this information they can go finish the registration and be able to log into the website and upload the file. I am going to be using MySQL to keep track of the users and would like to be able to verify their login information with MySQL each time they login. My question would be then, how difficult a task would anyone consider this for someone just starting out learning php? Does it seem like I am trying to do too much or is there an easy way of doing this that i might be missing? Any help would be great. Thanks.

to post a comment
PHP

38 Comments(s)

Copy linkTweet thisAlerts:
@coppocksDec 13.2006 — 
My question would be then, how difficult a task would anyone consider this for someone just starting out learning php?
[/quote]

Since your question is asking for an opinion, IMO, it's a considerable task for someone just starting out.

...is there an easy way of doing this that i might be missing?
[/quote]

There's no such thing as an "easy way" for a beginner. :eek: There's simply either "a BETTER way" or "THE way". 10 different PHP coders could easily write the code to that with 10 different approaches to achieve the same end. For an experienced coder, this is not a daunting task, but it's not a few lines of code and yer done either... in my opinion. ?
Copy linkTweet thisAlerts:
@TyreeDec 13.2006 — I second your opinion! ?
Copy linkTweet thisAlerts:
@NogDogDec 13.2006 — Probably your three options are:
[list=1]
  • [*]Spend some time (weeks, months, years?) learning programming and database theory and techniques in general, and PHP syntax and techniques in specific, and then writing your own application.

  • [*]Hiring an experienced programmer to do it for you now.

  • [*]Locate an existing script that has all the functionality you need and incorporate it into your web site.
  • [/list]

    If you pursue option 2 or 3, take some time to make sure you're choosing the right person or script, as there are very real security issues in what you're considering (you don't want a malicious user to be able to upload and then execute a malicious program on your web server, for example); so don't just go with the first "script kiddie" who says he'll do it all for $25.00, or install a free script from some site that provides no support, has no discussion forum, etc. where you can get help if needed.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Well, option 2 and 3 are not going to happen. I have been teaching myself php for the past few weeks and I have already learned a ton. I know what it is i have to do and I have a good idea how to do it. I was really looking for some suggestions on how to tackle it. What is there that I should be focusing on. I know that I don't need to know the in depth theory behind php or database design to get it to work. Eventualy I will know all there is to know about php and such. It isn't like I have no experience coding either. I know some (very little) C++, have taken a few classes on coding and such, taught myself enough javascript to be able to do stuff, and I have been hand coding websites for years. Like I said, I am looking for opinions and suggestions on a good way to approach this. Anyone want to offer some help, or suggestions?
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — I think the big problem here is this is not just a small portion you need help on. It's a whole system for which you have no start. That makes it very hard for someone to just help you out on the hard points. They basically have to hold your hand through the whole process. That's not meant to be an insult in any way. I'm sure you're a smart dude and a quick learner. But, it's a big time investment for someone to jump into.

    I'm more than willing to help you out in any way I can. I'm still learning myself and I have a php guru as a good friend...so I have a good "go to guy". I know it's bound to be tough when all you have is the net to help you!

    First thing you want to do is map out how you want to handle a user. Write out what the code will do verbosely, like following a play script. That'll help you begin to decide what methods to use in accomplishing your tasks as individual pieces to a whole.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Ok, well, I guess I did come across as if i knew nothing. Which isn't true. I have been getting help from people on this forum trying to get sample code i have to work the way I need it to. Right now I am just trying to get a simple form to e-mail me and several other people the inputs from a user. The code for the php that I have is this:

    [code=php]
    <?php
    function empMail($empEmail, $userName, $userEmail, $userMessage){
    $msg = $userName.' &lt;'.$userEmail.'&gt; has uploaded a file with the following message:' . "n";
    $msg .= $userMessage;
    $recipient = $empEmail;
    $subject = "File Upload Notification";
    $mailheaders = "From: ".$userEmail."n";
    $mailheaders .= "Reply-To: ".$userEmail;
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    }

    function userMail($userName, $userEmail, $userMessage){
    $msg = 'Your file was successfully uploaded and your message delivered. Below is the contents of your message:' . "n";
    $msg .= 'Your information: '.$userName. '&lt;'.$userEmail.'&gt;' . "n";
    $msg .= 'Your message: '.$userMessage;
    $recipient = $userEmail;
    $subject = "File Upload Notification";
    $mailheaders = "From: My Company <[email protected]>"."n";
    $mailheaders .= "Reply-To: My Company <[email protected]>";
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    }

    //define employee email address array
    $empEmails = array("[email protected]", "[email protected]", "[email protected]");

    //send email to user and employees if form was submitted correctly
    if ($_POST){
    if (!empty(strlen($name)) && !empty(strlen($email)) && !empty(strlen($message))){
    userMail($userName, $userEmail, $userMessage)."<br /><br />";
    foreach ($empEmails AS $empEmail){
    empMail($empEmail, $userName, $userEmail, $userMessage)."<br /><br />";
    }
    header("Location: http://www.someplace.com/success.php");
    } else {
    print "You must fill in all fields.";
    }
    }
    ?>
    [/code]


    and the html code is this:

    [code=html]
    <form id="sendmail" name="sendmail" method="post" action="sendmail.php">
    <p>Name:</p>
    <input type="text" name="userName" />
    <p>E-Mail:</p>
    <input type="text" name="userMail" />
    <p>Message</p>
    <textarea name="userMessage" cols="35" rows="10"></textarea>
    <br /><br />
    <input name="submit" type="submit" value="Send Mail" />
    </form>
    [/code]


    Now, that will probably look really familiar to some people because this is the code i got from someone else on this forum just trying to get it to send an e-mail. That is all I am trying to do now, next I am going to go to working with cookies to track a user and see where I have to go after that. I am trying to tackle it in small steps and learn as I go, get code to do what I need it to do, and then when I have finaly gotten all the peices to work on their own, bring them together and work out any kinks.

    So, if anyone can see what is wrong with that code let me know. I have been staring at it for a few days and I can't see what is going on. I think it is in the if statement at the end but I don't what exactly.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — That above is just a test I am trying to do to make sure I can actually do it. Once I get that to work I will modify it to work with the exact fields I need it to work with. Which should not be hard. Heck, that right there should not be hard either, but for some reason it just doesn't want to work. At first I was always getting the else statement, regardless of if there was information in the form fields or now. When i added the !empty() check it now just goes to a blank screen, like it is stuck in a loop or something.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — Yeah, looks REAL familiar to me...I've helped you with a ton of this. Still having trouble, eh? What's the problem with this part?
    Copy linkTweet thisAlerts:
    @coppocksDec 13.2006 — Well first of all, I dont see where the sendmail.php is getting the form POST array... You have variables in use, but they need to be defined (unless I am missing something!)... at the start of your script before the first function...

    $empEmail = $_POST['empEmail'];

    $userName = $_
    POST['userName'];

    $userEmail = $_POST['userEmail'];

    $userMessage = $_
    POST['userMessage'];

    That's the first most obvious thing I see.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — That was you? Lol.... yeah, I still can't get it to work. I either get it telling me that i need to fill in all fields (even when they are filled in) or it goes to just a blank screen (no errors or anything).
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Like, I said, I am still pretty new to php. I have been reading so many books, I guess I missed the part about having to initialize all the variables. So, what that does is tell the php that it needs to look for information coming from a form with the method POST?
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Ok, well I just added that in and I am still getting the same results. Now it seems like it is not doing anything at all. Even when I leave the form fields blank it just goes to a blank screen.
    Copy linkTweet thisAlerts:
    @coppocksDec 13.2006 — Yes, when you use the method=post, all fields get placed into and array/list called $_POST...

    $_POST['userName'] represents the field with that name, and returns the value entered in that field. You can use that as is, or create a variable from that, thats easier to use, ie, $userName = $_POST['userName'];

    Make sense?
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — You could have just replied to that thread...I'm here for ya! ?

    First, your if statement should be like this:
    [code=php]
    if ($_POST['submit']){
    if (strlen($name) && strlen($email) && strlen($message)){

    userMail($userName, $userEmail, $userMessage);

    foreach ($empEmails AS $empEmail){
    empMail($empEmail, $userName, $userEmail, $userMessage);
    }
    header("Location: http://www.someplace.com/success.php");
    } else {
    print "You must fill in all fields.";
    }
    }
    [/code]


    Then your form tag should be like this....IF you're going to use the same page. See my note below about coppocks post.
    <i>
    </i>&lt;form id="sendmail" name="sendmail" method="post" action="&lt;?php print $PHP_SELF; ?&gt;"&gt;


    coppocks mentioned that you need to define all these vars ahead of time. That may be a "best practice" type thing, but, as long as you're not changing pages, it's not necessary. I can show you 100 pages where I've used posted vars without defining them that way. Not that it HURTS to do it anyway! ?
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Ok, but doesn't strlen() just tell me how long the string is? Don't I want it to check if it is empty or not?
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — I use it in place of !empty()....it basically says that the var "has length". So, not empty. I just find it less confusing.


    If var is not empty then do...

    If var is SOMETHING then do...
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Ok, that is what i though it was doing but i was getting kind of confused. That said, I tried something else. I put the php into the same page as the form code and put action="<?php echo $_SERVER["PHP_SELF"];?>" into the form. Based on one of my many books that I am reading that is supposed to point the form to the same page as itself for the php code to do all the work, right? When i tried that the form just seemed to reset itself, and i did not get any e-mails in reply.

    So, when I have the php in another page i get the else statement saying i need to fill in all the fields and when i put it in the same page I just get the page to reset itself.
    Copy linkTweet thisAlerts:
    @coppocksDec 13.2006 — Well, there are a lot of generics in there... look at those email addresses... and there's no http://www.someplace.com/success.php for it to go to I'd imagine!

    So add some legit emails here so you can get 'em, ...

    $empEmails = array("[email protected]", "[email protected]", "[email protected]");

    And fix the header...

    header("Location: http://www.someplace.com/success.php");

    to a page on your server.

    C'mon Tyree, get him rolling here! ? ... looks like he's taking your example code literally!
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Well, no not exactly, i have my own e-mail address's and such in there, I just don't post them here with the code, because frankly I don't want to be giving out those e-mails to the entire internet at one time. lol.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — I'm going as fast as I can...I was just about to ask him if he was putting in actual email addresses! ? I've got a day job too, ya know?! ?

    Okay, so you are putting in real email addresses...

    So, the code you're using looks JUST like what you posted other than the email addresses?

    Have you updated the if statement like I showed?

    Holy crap....it's the if statement. It's looking for $name, $email and $message...but you've got your form inputs set to userName, userEmail and userMessage. Change on or the other so they match. ?

    My fault...I should have seen that way sooner! ?

    EDIT: Definitely change the if statement rather than the form. The user* vars are used in several places.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — I should have seen that! Like i said, I knew it was in the if statement somewhere, i just wasn't seeing it. Let me try that and see if I can get it to work.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — I tell ya...sometimes you just look at code SOOO much that you can no longer read anything. Ugh! ?
    Copy linkTweet thisAlerts:
    @coppocksDec 13.2006 — DOH! I didn't see it either! Weeee! LOL! That was fun! LOL! I need to get my eyes off code for a while!
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Well, it was a valiant try, but it is still spitting back that i need to fill in all fields.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — Let's see the code again with the changes to be sure.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Here is the changed php code:

    [code=php]
    <?php

    $empEmail = $_POST['empEmail'];
    $userName = $_POST['userName'];
    $userEmail = $_POST['userEmail'];
    $userMessage = $_POST['userMessage'];

    function empMail($empEmail, $userName, $userEmail, $userMessage){
    $msg = $userName.' &lt;'.$userEmail.'&gt; has uploaded a file with the following message:' . "n";
    $msg .= $userMessage;
    $recipient = $empEmail;
    $subject = "File Upload Notification";
    $mailheaders = "From: ".$userEmail."n";
    $mailheaders .= "Reply-To: ".$userEmail;
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    }

    function userMail($userName, $userEmail, $userMessage){
    $msg = 'Your file was successfully uploaded and your message delivered. Below is the contents of your message:' . "n";
    $msg .= 'Your information: '.$userName. '&lt;'.$userEmail.'&gt;' . "n";
    $msg .= 'Your message: '.$userMessage;
    $recipient = $userEmail;
    $subject = "File Upload Notification";
    $mailheaders = "From: insert real e-mail info here"."n";
    $mailheaders .= "Reply-To: insert real e-mail info here";
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    }

    //define employee email address array
    $empEmails = array("[email protected]", "[email protected]", "[email protected]");

    //send email to user and employees if form was submitted correctly
    if ($_POST){
    if (strlen($userName) && strlen($userEmail) && strlen($userMessage)){
    userMail($userName, $userEmail, $userMessage)."<br /><br />";
    foreach ($empEmails AS $empEmail){
    empMail($empEmail, $userName, $userEmail, $userMessage)."<br /><br />";
    }
    header("Location: http://www.somewhere/success.php");
    } else {
    print "You must fill in all fields.";
    }
    }
    ?>
    [/code]


    The html code is still the same
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — I am beginning to wonder if there is a problem with my php.ini file and the way i have that configured. I just tried another different sample code from a book that should work as is, I just retyped everything exactly as it was written in the book and that doesn't work either.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Is anyone familiar with running php5.2 on windows 2003 server, with MySQL, and IISv6.0? I have followed the php manual instructions to a "t" and it seems to work, when I run phpinfo() I get it pointing to the right place and all. Are there some modules that i need to turn on or something? This isn't making sense, really simple php codes works, like spiting stuff back out to the screen but when ever I try to do anything complicated like read info from a form and mail it or even just try to upload a file nothing happens.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — Still need to change the if statement like I showed you a couple posts back...
    [code=php]
    if ($_POST['submit']){
    if (strlen($userName) && strlen($userEmail) && strlen($userMessage)){
    userMail($userName, $userEmail, $userMessage);
    foreach ($empEmails AS $empEmail){
    empMail($empEmail, $userName, $userEmail, $userMessage);
    }
    header("Location: http://www.somewhere/success.php");
    } else {
    print "You must fill in all fields.";
    }
    }
    [/code]


    Define which post var is the condition...and drop the <br> tags...that was just for when I was printing it to prove it was working. ?
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Oh!!!
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — And, still nothing. Still saying that i need to fill in all fields.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — This is the code I just used on my site. It worked perfectly to all 4 addresses.
    [code=php]
    <?php

    $empEmail = $_POST['empEmail'];
    $userName = $_POST['userName'];
    $userEmail = $_POST['userEmail'];
    $userMessage = $_POST['userMessage'];

    function empMail($empEmail, $userName, $userEmail, $userMessage){
    $msg = $userName.' <'.$userEmail.'> has uploaded a file with the following message:' . "n";
    $msg .= $userMessage;
    $recipient = $empEmail;
    $subject = "File Upload Notification";
    $mailheaders = "From: ".$userEmail."n";
    $mailheaders .= "Reply-To: ".$userEmail;
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    }

    function userMail($userName, $userEmail, $userMessage){
    $msg = 'Your file was successfully uploaded and your message delivered. Below is the contents of your message:' . "n";
    $msg .= 'Your information: '.$userName. '<'.$userEmail.'>' . "n";
    $msg .= 'Your message: '.$userMessage;
    $recipient = $userEmail;
    $subject = "File Upload Notification";
    $mailheaders = "From: insert real e-mail info here"."n";
    $mailheaders .= "Reply-To: insert real e-mail info here";
    //send the mail
    mail($recipient, $subject, $msg, $mailheaders);
    }

    //define employee email address array
    $empEmails = array("address1", "address2", "address3");

    //send email to user and employees if form was submitted correctly
    if ($_POST['submit']){
    if (strlen($userName) && strlen($userEmail) && strlen($userMessage)){
    userMail($userName, $userEmail, $userMessage);
    foreach ($empEmails AS $empEmail){
    empMail($empEmail, $userName, $userEmail, $userMessage);
    }
    header("Location: http://www.tyreeonline.com/main.php");
    } else {
    print "You must fill in all fields.";
    }
    }
    ?>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <form name="sendmail" method="post" action="<?php print $PHP_SELF ?>" >
    <table width="300" border="0" cellspacing="0" cellpadding="3">
    <tr>
    <td align="right" valign="top">Name:</td>
    <td><input type="text" name="userName" value="<?php print ((strlen($userName))? $userName : '')?>" /></td>
    </tr>
    <tr>
    <td align="right" valign="top">Email:</td>
    <td><input type="text" name="userEmail" value="<?php print ((strlen($userEmail))? $userEmail : '')?>" /></td>
    </tr>
    <tr>
    <td align="right" valign="top">Message:</td>
    <td><textarea name="userMessage" cols="35" rows="5"><?php print ((strlen($userMessage))? $userMessage : '')?></textarea></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td><input type="submit" name="submit" value="Send Email" /></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    [/code]



    Pay attention to how I did the form too. It makes it where the fields aren't blank if there's an error. So, the user doesn't have to fill in the blanks again.

    Also, I changed the "&lt;" and "&gt;" in both functions. They didn't parse in my email. I just made them into <>.

    Oh, make sure you change this, "$mailheaders = "From: insert real e-mail info here"."n"; " in the userMail function too.
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Ok, what the heck, I copied that over and it worked fine, but that is almost exactly like what i have right? I put in my e-mails and stuff and pointed the header to my success.php page and it loaded everything fine.
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — Hmmm, yeah, I just copied your code in, changed a few things and it worked.


    Here's what I changed...

    The if statement...dropped the <br>'s and changed the conditional to

    if ($['submit']) {

    I put in my email addresses for the $empEmails array. Nothing should have been wrong there.

    Other than that...yeah, it all should have been the same. Could've been something as small as a period or semi-colon though that through it out of whack. You'd have to study the two line for line to find it! ?

    At least it works, right?! ?
    Copy linkTweet thisAlerts:
    @polorboyauthorDec 13.2006 — Alright, I am going to go look over your code and my code and see what differences I can find. Even though the code worked, and it went to my success.php page, I still have not gotten a e-mail yet. That could by my php.ini file and the smtp settings. Well, I will go through and see what I can find different in the two. Thanks though, at least I got a working piece of code to work with. ?
    Copy linkTweet thisAlerts:
    @TyreeDec 13.2006 — Well crap...still not working completely, eh? Yeah, gotta be something besides the code. Because I can test the code all day long and it work son my server! ?
    Copy linkTweet thisAlerts:
    @polorboyauthorJan 03.2007 — Just thought I would let you guys know, I figured out what was wrong with my script. I just had some syntax errors. It works great now.
    Copy linkTweet thisAlerts:
    @TyreeJan 03.2007 — Weird...wonder what it was. I was using the same code on my server and it was working. Must have been one of the lines that deals with server specific variables, huh?

    Glad it's working finally! ?
    ×

    Success!

    Help @polorboy spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.22,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...