My first PHP code!

Discussion in 'Web Design and Development' started by dmmcintyre3, Aug 26, 2009.

  1. Coolnat2004 macrumors 6502

    Coolnat2004

    Joined:
    Jan 12, 2005
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #3
    I think you need to write correct HTML before you work with PHP. The HTML you provide is completely malformed. Your missing head and body tags, misusing the title tag among other things.
     
  3. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #4
    Not only that, but two suggested minor improvements in your PHP code:

    1) Test for a variable existing in the current scope before testing a value as part of an if/then/else, case or switch.

    Syntax Example:

    PHP:
    if (isset($_GET['s']) && empty($_GET['s'])) { ...etc... } else { ...etc...}
    2) Only local users on your server can hear the speaker, so this is not a good Internet app. On a side note you code assumes the say binary is accessible without a path prefix and exists on the server and will execute properly 100% of the time, regardless of the user supplied input string.

    Might want to look into return variable argument for the exec command and check if false i.e.:

    PHP:
    exec ("say ".$_GET['s'], $output$status);
    if (
    $status===false) { echo "Sorry, the 'say' program crashed!"; }
    Or check the manual for say and see what error response codes it gives based on common errors and check for/trap those. I'm just showing explaining the concept, up to you to code it. Bottom line is good developers always check for errors on everything - so your script degrades gracefully. Consider it a prime directive in coding in general, is my point here.

    Overall, I suggest you study up on HTML, and validate every script using the W3C markup validator service to identify and fix obvious markup errors. That has nothing to do with PHP.

    My .02

    -jim
     
  4. Joined:
    Jun 30, 2009
    #5
    My advice would be to get yourself BOOKS. I don't know where you smart-ass guys learn from, but you end up screwing everything up, although going around saying you "can makes websites".

    So if you ever decide on some books, have a look over O'Reily titles (especially Head First, they work awesomely for beginners).

    As for your code, apart from the fact that you know nothing about how these things work, your style of coding is pure RUBBISH. Get yourself a nice text editor (say TextWrangler or TextMate), and start writing more intelligible code. Not to mention that you are trying to get into certain concepts you have no idea about, by improvising. But in a malformed manner. Hint: functions @title.
     
  5. Dal123 macrumors 6502a

    Dal123

    Joined:
    Oct 23, 2008
    Location:
    England
    #6
    A little rude and extremely condescending :mad:.
     
  6. Joined:
    Jun 30, 2009
    #7
    I'm sorry, but if you really want to learn something, I'm extremely confident there is enough material on the web to get you started. But there are a couple of threads here which show the exact opposite, so there are plenty of reasons to be rude and extremely condescending.
     
  7. joshysquashy macrumors 6502a

    Joined:
    May 13, 2005
    Location:
    UK
    #8
    I learnt everything I know from the web (and trial and error!). You don't need books, but if it's your preferred format then go ahead.

    The web is the greatest source of knowledge for the web! All the articles written by web designers, for web designers are a godsend!

    A good/bad thing is that PHP won't run if it is malformed, but HTML will, so watch out for that!

    Also, PHP will run the same in every browser, but make sure you view your HTML in every browsers. Safari is pretty tolerant of errors, but IE isn't (and doesn't obey standards either).

    Just imagine a world without IE :rolleyes:
     
  8. Joined:
    Jun 30, 2009
    #9
    Good for you, because I find it hard to learn PHP, SQL and JS from the web, I bought two books to get me started, couldn't be more satisfied.

    To clarify a few things, PHP is dependent of the web server (mainly, Apache and IIS, the latter being built by Microsoft, and, if I'm not wrong, platform-dependent -- that is, Windows). So PHP does not display at all. It simply generates HTML code, which can be good or bad, valid or malformed.
     
  9. dmmcintyre3 thread starter macrumors 68020

    Joined:
    Mar 4, 2007
    #10
    New version, 2 posts down

    I added code to block that!
    The old code looked fine in IE and so does the new code
     
  10. Joined:
    Jun 30, 2009
    #11
    I suggest using a Strict header (mainly, XHTML 1.0 Strict). This way, <iframe> will become invalid. And if you want pure and correct XHTML, you have to use Strict, not Transitional.

    Also, you're not making your code easy to read. Writing an entire HTML file on one line, no matter how short it is, can be considered a joke. Also the spacing is non-existent.

    Overall, you haven't specified any reason behind the thread. Ok, this is your first PHP code. But it's not looking nice at all. Although it works, it's a complete mess, I really appreciate you if you actually understand what you're writing there...
     
  11. dmmcintyre3 thread starter macrumors 68020

    Joined:
    Mar 4, 2007
    #12
    New version now with working old file deleter:

    If you open the generator without flags:
    HTML:
    <!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><title>What do you want to say?</title>
    </head>
    <body>
    <h1>What do you want to say?</h1><!-- more of the interface -->
    <form action="index.php" method="get">
    <input type="text" name="s" />
    <input type="submit" />
    </form>
    </body>
    </html>
    If you open it with flags: (?s=is+my+HTML+valid?)
    HTML:
    <!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>
    	<title>Saying: is my html valid</title>
    	</head>
    	<body> 
    	<h1>The computer just said: is my html valid</h1> 
    	<br /><br />
    	<a href="index.php"> To say something else click here </a><a href="rec/ismyhtmlvalid.aiff" > Download ismyhtmlvalid.aiff</a> <br/> <iframe src= "rec/ismyhtmlvalid.aiff" height="20" frameborder="0" /><!-- more of the interface -->
    <form action="index.php" method="get">
    <input type="text" name="s" />
    <input type="submit" />
    </form>
    </body>
    </html>
    Here is the updated PHP code:
    PHP:
    <!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">
    <?php

    #Security Measures:
    $_GET['s']=preg_replace('/[^0-9a-z , \' .  -]+/i'''$_GET['s']);

    #Filename Fix
    $filename $_GET['s'];
    $filename=preg_replace('/[^0-9a-z]+/i''',  $filename);

    #remove old recordings
    // Define the folder to clean
    // (keep trailing slashes)
    $captchaFolder  'rec/';
     
    // Filetypes to check (you can also use *.*)
    $fileTypes      '*.aiff';
     
    // Here you can define after how many
    // minutes the files should get deleted
    $expire_time    20
     
    // Find all files of the given file type
    foreach (glob($captchaFolder $fileTypes) as $deleterFilename) {
     
        
    // Read file creation time
        
    $FileCreationTime filectime($Filename);
     
        
    // Calculate file age in seconds
        
    $FileAge time() - $FileCreationTime
     
        
    // Is the file older than the given time span?
        
    if ($FileAge > ($expire_time 60)){
     
            
    // Now do something with the olders files...
     
            #print "The file $deleterFilename is older than $expire_time minutes\n";
            
    if ( $deleterFilename == '' ) ;{ 
            
    $fdel 'none';
            }}
        else ;
            {
            
    // For example deleting files:
            
    unlink($deleterFilename);
            
        
        }
     
    }

    #The interface 
    if ($_GET['s'] == ''){
    echo 
    "<head><title>What do you want to say?</title>
    </head>
    <body>
    <h1>What do you want to say?</h1>"
    ;
    }else{
    echo 
    "
        <head>
        <title>Saying: "
    .$_GET['s']."</title>
        </head>
        <body> 
        <h1>The computer just said: "
    .$_GET['s']."</h1> 
        <br /><br />
        <a href=\"index.php\"> To say something else click here </a>"
    ;

    # The Text to Speech engine
    exec ('touch rec/'.$filename.'.aiff');
    exec ('say -o ./rec/'.$filename.'.aiff '.$_GET['s']);
    echo (
    '<a href="rec/'.$filename.'.aiff" > Download '.$filename.'.aiff</a> <br/>
    <iframe src= "rec/'
    .$filename.'.aiff" height="20" frameborder="0" />') ;} ?>
    <!-- more of the interface -->
    <form action="index.php" method="get">
    <input type="text" name="s" />
    <input type="submit" />
    </form>
    </body>
    </html>



     
  12. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #13
    Without reading the thread, my guess is its deserved. There's way too many "programmers" out there who don't know what they're doing. If you want to get good, you need to take criticism.
     
  13. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #14
    Still trying to understand why this thread exist. Are you wanting feedback on programming style? Is something not working right you want help fixing? So far it has been very random and I feel no motivation to add much because you have made no effort to say what this thread is about.
     
  14. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #15
    Looks like the OP is excited to complete their first PHP program, not realizing that we scrutinize everything that's posted. The OP actually should be credited for reading all the replies and putting in the effort to recode, even if it's not perfect and the subject chosen is somewhat silly. Being this is a learning forum, the thread does serve as a wacky but valid example of script revision for beginner development. And a warning to beginners - the HTML matters as much as the PHP, and put a little more effort into it from the get go. The OP won't like it, but its true. I'm not saying it's the most exciting for the rest of us, of course. I personally found this topic boring.
     
  15. Joined:
    Jun 30, 2009
    #16
    While trying to give him a reading on Writing Beautiful HTML Code, I found and bookmarked this. It's awesome for a beginner!
     
  16. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #17
    Seriously? You found that slideshow useful? I thought it was badly made (several slides had the words cut off at the top and bottom), painful to sit through, and I was slightly insulted by the way the author chose to repeat statements for emphasis.
    repeat statements for emphasis.
    repeat statements for emphasis.

    To each his/her own I guess.
     
  17. Joined:
    Jun 30, 2009
    #18
    The coding style he shows is very good, and the text is not important, but the actual code. Which really is well-written and he also explains (very briefly) the separation between presentation and content at the end.
     

Share This Page