www.webdeveloper.com
Results 1 to 6 of 6

Thread: My First PHP Script.

  1. #1
    Join Date
    Mar 2010
    Posts
    5

    Smile My First PHP Script.

    Im enjoying this scripting for websites. but i need to try new things more and more. Ive only just made a script for javascript which does login, so i thought i'd try and make a login script for php, since its more secure? If it isnt secure can you tell me another language to use to make a secure login page.
    Anyway take a look and comment back please.

    PAGE: LOGIN2.PHP
    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>This is my new login document.</title>
    </head>
    
    <body>
    <form action="login3.php" method="POST">
    <center>
    Username: <input type="text" size="20" name="username" />
    <br />
    <br />
    <br />
    Password: <input type="password" size="20" name="password" />
    <br />
    <br />
    <input type="submit" value="Login" name="submit" />
    <br />
    <br />
    <Br />
    </center>
    </form>
    </body>
    </html>

    PAGE: LOGIN3.PHP
    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Login</title>
    </head>
    
    <body>
    <?php
    //////////////////////////
    $u = $_POST['username'];
    $p = $_POST['password'];
    //////////////////////////
    
    //////////////////////////
    //SET USERNAMES AND PASS//
    //////////////////////////
    $u1 = 'username1';
    $p1 = 'password1';
    
    $u2 = 'username2';
    $p2 = 'password2';
    
    $u3 = 'username3';
    $p3 = 'password3';
    
    
    
    
    // THE SCRIPT
    
    if ($u == $u1) $pass = $p1;
    if ($u == $u2) $pass = $p2;
    if ($u == $u3) $pass = $p3;
    
    if ($pass === $p) {
    	echo("You are logged in as: ". $_POST['username']);
    }
    else {
    	echo("Invalid Username and Password.");
    }
    ?>
    
    
    
    
    
    </body>
    </html>

  2. #2
    Join Date
    Mar 2010
    Posts
    5
    Question, please answer.

    Also, can I have multiple PHP scripts on one page, sharing variables?

    Say I had my login script at the top, but then I have a new PHP script at the bottom say this:

    Code:
    <?php
    echo($u);
    ?>
    Would that write the Username from the above script? Or do I have to reset all the variables again?

    Thanks.

  3. #3
    Join Date
    Mar 2010
    Posts
    5
    I don't know how to edit threads on this forum. Sorry for multiple posts...

    Updated login form:
    Code:
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>
    <?php
    //////////////////////////
    $u = $_POST['username'];
    $p = $_POST['password'];
    //////////////////////////
    
    //////////////////////////
    //SET USERNAMES AND PASS//
    //////////////////////////
    $u1 = 'username1';
    $p1 = 'password1';
    
    $u2 = 'username2';
    $p2 = 'password2';
    
    $u3 = 'username3';
    $p3 = 'password3';
    
    
    
    
    // THE SCRIPT
    
    if ($u == $u1) $pass = $p1;
    if ($u == $u2) $pass = $p2;
    if ($u == $u3) $pass = $p3;
    if ($_POST['username']) {
    if ($pass === $p) {
    	echo("You are logged in as: ". $_POST['username']);
    	$title = "2";
    }
    else {
    	echo("Invalid Username and Password.");
    	$title = "1";
    }
    }
    else {
    	$title = "1";
    }
    ?>
    </title>
    </head>
    
    <body>
    <?php
    if ($title == 1) {
    	echo("
    <form action='login2.php' method='POST'>
    <center>
    Username: <input type='text' size='20' name='username' />
    <br />
    <br />
    <br />
    Password: <input type='password' size='20' name='password' />
    <br />
    <br />
    <input type='submit' value='Login' name='submit' />
    <br />
    <br />
    <Br />
    </center>
    </form>");
    }
    ?>
    </body>
    </html>

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by LA33R View Post
    Question, please answer.

    Also, can I have multiple PHP scripts on one page, sharing variables?

    Say I had my login script at the top, but then I have a new PHP script at the bottom say this:

    Code:
    <?php
    echo($u);
    ?>
    Would that write the Username from the above script? Or do I have to reset all the variables again?

    Thanks.
    Yes you can have multiple php code blocks within a html file. The code in each php block will be executed wherever it appears in the html.

    Also, I would store all my username and passwords in a database on the server, which makes them much more secure than coding them in your script.

    Apart from the security issue, if you code your valid usernames and passwords in your script, you should also think about how do you plan to allow visitors to register a new account and how do you plan to allow them to close their accounts.

  5. #5
    Join Date
    Mar 2010
    Posts
    5
    Good idea. this was just a test script as it was the first one ive tried, Might look into databases tonight. How do i do it, would i make a access database on my computer and upload it to the server for it to change?

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles