Hello & Welcome to our community. Is this your first visit? Register
Follow us on
Follow us on Facebook Follow us on Twitter Watch us on YouTube


MMOCoin

Likes Likes:  0
Results 1 to 3 of 3
  1. #1
    Scout

    Join Date
    Jul 2009
    Posts
    20
    Post Thanks / Like
    Rep Power
    16
    Reputation
    40

    [ Php ]Get And Post


    Register to remove this ad
    Php Get and Post

    Lecture

    Introduction

    In php you have alot of functions that you use many over and over again, but the 2 i've come to use
    the most are the '$_GET' and '$_POST' functions. I have just mislead you by calling them functions
    though since they are not. They are part of the group of called 'superglobal variables'. 'Superglobal
    variables' are predefined variables in PHP, and they are available in all scopes throughout a script.
    Here is a list of all the global variables.


    • $GLOBALS
    • $_SERVER
    • $_GET
    • $_POST
    • $_FILES
    • $_COOKIE
    • $_SESSION
    • $_REQUEST
    • $_ENV



    What Is It For?

    The get and post variables are for sending data from one page to another. For example you have a form
    with a textbox named 'name'. When you click the button in the form, it will pass what ever data is in
    the textbox to the next page and using get and post you must retrieve it. This can come in handy in
    many scenarios, that involve user information needing to be send into a database.

    My Personal Method

    I don't know if everyone does it like this, if its just me, or if a few others do or not, but I use get
    and post each in a certain way though they can be used in the exact same manner as one another. In my
    opinion putting data inside the url (ex. 'localhost/index.php?user=info&pass=moreinfo') looks exetremely
    tacky and unorganized, as well as hard to memorize. I like my users to understand what the url says as
    well, i want them to be able to come back to an exact page easily. So i use post when passing data, but
    when handling stuff such as a webpage with mulitple outputs i would use get.

    Whats The Difference

    There is a small but at the same time a very major difference in get and post. When using get all the
    information being gained must be taken from the url, thus giving you long url names. Though post is
    completely hidden from all user eyes(comes in handy for passing a password). You may say, 'well why not
    always use post'. Simple answer there, you can't, post can only be accessed when passing on data inside
    of a form. Meaning that in order to make your websites better, your gonna have to manage to mix in a bit
    of both.

    Coding

    Introduction

    We all know that the lecture part of anything is boring but its also always necessary. Alot of tutorials
    just give the code instead of explain it. In doing so, leaves the readers confused to what they're doing
    and unable to help others with the knowledge. Now that that parts over lets get into the code though.
    In this tutorial were gonna be developing a login system, but the system will not be complete until my
    next tutorial. In this tutorial were gonna learn the basics of multipages inside of one file, and sending
    and echoing data through the server.

    Main Login Form

    Okay to start were gonna need to design the page, lucky for you i've already done that since this is a php
    tutorial not a css one i don't wanna stray from what were here to learn.

    Code:
    <?php
    //Define data here
    
    ?>
    
        <html>
    
        <head>
        <title>Login</title>
        <style type='text/css'>
    
        #wrapper
        {
        margin-top:130px;
        }
    
        #user
        {
        }
    
        #pass
        {
        }
        </style>
        </head>
    
        <body>
    <?php
    $HTML .="    <form action='login.php?m=1' method='post'>
        <div id='wrapper'>
        <center>
    
        <div id='user'>
        Username:
        <input type='text' name='user' size='25' />
        </div>
        <br />
        <div id='pass'>
        Password:
        <input type='password' name='pass' size='25' />
        </div>
        <input type='submit' value='login' /> </center></div>
        </form>";
    
    echo $HTML;
    ?>
    
    
        </body>
        </html>
    The most important line in this to allow us to use our get and post effectively.
    Code:
    <form action='login.php?m=1' method='post'>
    the form tag is what we use, action is what page were going to notice 'login.php?m=1'.
    m is the variable inside of $_GET that we will be accessing.

    were gonna use $_GET to check if the data has been sent or not.

    lets get inside of our first php code area

    Code:
    <?php
    //Define data here
    
    ?>
    We were gonna change the code inside of there to this

    Code:
    $MODE = $_GET['m'];
    now that we have gained the value of 'm' and saved it as a variable '$MODE' lets access it to.
    in the second php code area, lets add this above the other code

    Code:
    if($MODE != null || $MODE != "")
    {
    $HTML .= "<p> Your data has been sent to this form</p>";
    }
    run your code in your php server to see the example.
    type in some random info into the 2 textboxs and click submit.

    notice the url changes but the only difference was 'Your data has been sent to this form' was
    output to the top of it.

    excellent now we know whether or not the data is sent but, we haven't accessed the data that the
    form passed. To do so we will be making use of $_POST.

    Lets go to our first php code area and under the code already placed lets add this.

    Code:
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    its the same exact syntax as get

    now lets output our user info to ourself by adding this in our if function

    Code:
    $HTML .= "<p> Your username is " . $user . "</p>";
    $HTML .= "<p> Your password is " . $pass . "</p>";
    run your code and see the effect of the script.

    This isn't a login system though because it doesn't check another username/password for a match
    since this is just a tutorial were gonna use some custom defined user info.

    add this under your defines
    Code:
    $loguser = "username";
    $logpass = "password";
    them just sitting there doesn't show us anything were gonna need to use if functions as comparisons
    to make sure the data matches if it does match, then it will login, if not state the error.

    lets start by checking to make sure no fields were left blank replace this with the code inside the if

    Code:
    if($user == null || $user == "")//Checks if username is empty or not
    {
        $HTML .= "You left your username blank<br />";//outputs if username is empty
    }
    else if($pass == null || $pass == "")//Checks if password is empty or not
    }
        $HTML .= "You left your password blank<br />";//outputs if password is empty
    }
    else if($user != $loguser)//Checks if username is correct
    {
        $HTML .= "Your username is incorrect<br />";//outputs if username is incorrect
    }
    else if($pass != $logpass)//Checks if password is correct
    {
        $HTML .= "Your password is incorrect<br />";//outputs if password is incorrect
    }
    else//If all ifs fail then login info is correct and login succeeds
    {
        $HTML .= "Login Successful<br />";
    }
    the code is explained in the comments, and there we have a functional login system, keep looking around
    for my next tutorial, which will explain creating the session to keep them logged in and use mysql to
    store accounts and retrieve info from there.


    › See More: [ Php ]Get And Post

  2. #2
    Founder
    Apple's Avatar
    Join Date
    Jul 2008
    Location
    HeaveN
    Posts
    15,916
    Post Thanks / Like
    Rep Power
    10
    Reputation
    295
    nice one : )





  3. #3
    Premium

    Join Date
    Nov 2012
    Posts
    2
    Post Thanks / Like
    Rep Power
    13
    Reputation
    2

    Register to remove this ad
    This does not require MySQL, true, but if you're going to use MySQL don't forget to escape your variables.
    $user = $mysqli->real_escape_string($_POST['user']);
    $pass = $mysqli->real_escape_string($_POST['pass']);

    Where $mysqli is your MySQLi connection.




  4. Related Threads - Scroll Down after related threads if you are only interested to view replies for above post/thread

 

 

Visitors found this page by searching for:

Nobody landed on this page from a search engine, yet!
SEO Blog

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -5. The time now is 08:33 AM.
Powered by vBulletin® Copyright ©2000-2024, Jelsoft Enterprises Ltd.
See More links by ForumSetup.net. Feedback Buttons provided by Advanced Post Thanks / Like (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
vBulletin Licensed to: MMOPro.org