kakajos2000 Posted July 8, 2008 Share Posted July 8, 2008 Been strruggling to understand the whole concept of the GET variable. I am comfortable with post, but GET is something else. Especially when it comes to "Passing variables through the URL". http://www.phpfreaks.com/forums/index.php?action=post;board=65.0 My apologies for copying and pasting the above address, which is currently what I have on my browser, but just how did it get to this? How can I generate such addresses in my website? I need a step-by-step, kindergatten-paced assistance on this headache. All assistance will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/113707-get-is-a-headache-need-help-here/ Share on other sites More sharing options...
redbullmarky Posted July 8, 2008 Share Posted July 8, 2008 Hi and welcome to the forums Please read the board descriptions before posting - OOP Help is for Object Oriented Programming not general PHP help (moved). To answer your question - the URL you've pasted is not a great example for a beginner, as it's handled by a custom interpreter (don't worry about that for now!). A general query string is appended to your URL using a ? and each variable key/value pair is seperated by an ampersand (&). For example: www.example.com/index.php?user=1&page=2&something=hello+world in your index.php script, you can access these variables by: <?php echo $_GET['user']; // outputs 1 echo $_GET['page']; // outputs 2 echo $_GET['something']; // outputs 'hello world' ?> notice the + between hello and world in my example link - this denotes a space, as regular spaces are not part of a valid URL. By all means read up on urlencode() and urldecode() for more info on forming valid URLs, but you probably shouldn't trouble with yourself just yet. Back to the original link: There are little tricks that one can use to improve search engine rankings. Generally, query strings containing lots of variables are not very search engine friendly, as are those that contain certain characters. However - assuming there are no rewrite tricks set up, doing: echo $_GET['action']; would output: post;board=65.0 which is then handled by an internal SMF (the forum software) script that would extract the info needed to build the page. so rather than having 2, 3, 4 or loads more variables, there's just the one. In a nutshell - rather than using the default way of handling GET parameters, SMF uses its own methods to improve search engine friendliness. Hope that helps a bit. Quote Link to comment https://forums.phpfreaks.com/topic/113707-get-is-a-headache-need-help-here/#findComment-584338 Share on other sites More sharing options...
kakajos2000 Posted July 8, 2008 Author Share Posted July 8, 2008 Thanks a lot, Sir, but I wish I could claim to have understood. What I am reeeeeally struggling with is the whole concept: In POST, you receive data from a form, right? Can I also use GET to get input, e.g form input? I must be the biggest dunderhead ever, cz I have struggled with this GET thing for weeks now. Supposing I have this simple script: <?php $user=$_POST['username']; $pass=$_POST['password']; $email=$_POST['email']; $message=$_POST['message']; $subject=$_POST['subject']; $to='kakajos2000atyahoodotnet'; $from='from: $email/r/n'; mail($to, 'message from kakajos', $subject, $message, $from); ?> Now, just how would GET come in? Would I use GET with form input? I understand there's a limit to the amount of xters that GET can handle. That's my 2cents worth of thinking. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/113707-get-is-a-headache-need-help-here/#findComment-584354 Share on other sites More sharing options...
mbeals Posted July 8, 2008 Share Posted July 8, 2008 to the processing script, GET and POST are basically the same thing. They are just arrays (with different names). The difference is in the page leading up to the processing script. With a form, you can choose between using post or get in the form 'method' attribute <form action="" method="get"></form> // uses GET array <form action="" method="post"></form> // uses POST array The advantage to GET is that you can set the variables in the URL. This lets you pass dynamic data to the script without using forms at all. The forum is a good example of this. It has a list of topics that simple hyperlinks. When you click on a link, it passes some data to a script that retrieves the messages from the database and prints them to the screen. This is performed without any forms. Quote Link to comment https://forums.phpfreaks.com/topic/113707-get-is-a-headache-need-help-here/#findComment-584369 Share on other sites More sharing options...
craygo Posted July 8, 2008 Share Posted July 8, 2008 $GET and POST do pretty much the same thing, it is just how the values are past which is different. Get has a much smaller limit on the amount of data that can be transfered and makes it easy to see what is getting past because you can just look at the url and see. POST works better for large data updates and file transfers. in a simple form <form name="somename" action="somepage.php" method="GET" /> <input type="text" name="firstname" /> all values between the form tags will now be passed through the url. so lets say we gave this input a value of Ray the url will be http://somesite.com/somepage.php?firstname=Ray so now you assign your variables $firstname = $_GET['firstname']; If you simply change the method to POST the url would be different and you would assign the variable differently http://somesite.com/somepage.php $firstname = $_POST['firstname'] Now sometime there is a need to use POST or GET for certain situations like hidden variables and pages which do a bunch of different things based on something in the url. You can have one page which can process mysql tasks like add, edit,update,insert all based off a value in the post values or a value in the url(GET). Like in the address you gave above, the page is using POST to insert your thread and replies and things of that nature but it using the GET to tell the page what task to do action=post Ray Quote Link to comment https://forums.phpfreaks.com/topic/113707-get-is-a-headache-need-help-here/#findComment-584372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.