{"id":287,"date":"2018-01-19T19:18:51","date_gmt":"2018-01-19T19:18:51","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/getting-started-passing-variables\/"},"modified":"2018-01-19T19:18:52","modified_gmt":"2018-01-19T19:18:52","slug":"getting-started-passing-variables","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/getting-started-passing-variables\/","title":{"rendered":"Getting Started Passing Variables"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By PHP Builder Staff<\/div>\n<div class=\"\">on March 2, 2012<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\">A lot of beginning developers don&#8217;t understand that the web is &#8220;Stateless&#8221;. That means that the web server forgets who you are as soon as it sends you a page. So if you&#8217;re logged in as John Doe and viewing your bank account, you need to send the web server your user ID for each page that you view. The script on the web server then needs to take that user ID and verify that it is authentic for every single page. It&#8217;s a lot of work, but it has to be done. There are libraries like PHPLib that will handle a lot of it for you, but I have always done the authentication myself, rather than using a general purpose library.<\/p>\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\">Anyway, a URL might look like this:<\/p>\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\"><strong>https:\/\/www.bigbank.com\/view_account.php3?ID=1511&amp;token=aBCcdEfgh<\/strong><\/p>\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\">Inside of the<span class=\"Apple-converted-space\">\u00a0<\/span><strong>view_account.php3<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>script, you would be conveniently handed two variables:<span class=\"Apple-converted-space\">\u00a0<\/span><strong>$ID<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>and<span class=\"Apple-converted-space\">\u00a0<\/span><strong>$token<\/strong>. You could then do a query on the database to make sure the token was legitimate and then display the information for<span class=\"Apple-converted-space\">\u00a0<\/span><strong>$ID<\/strong>. Just do a query like what I showed on the &#8220;Get Started With Databases&#8221; page.<\/p>\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\">Passing variables with forms is very similar. You use<span class=\"Apple-converted-space\">\u00a0<\/span><strong>form fields<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>to accomplish a lot of it:<\/p>\n<pre style=\"font-family: courier,sans-serif; font-size: 10px; width: 490px; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; background-color: #ffffff;\"><span style=\"font-family: COURIER;\">\n&lt;FORM ACTION=\"my_page.php3\" METHOD=\"get\"&gt;\n&lt;INPUT TYPE=\"TEXT\" NAME=\"name\" VALUE=\"\"&gt;\n&lt;INPUT TYPE=\"PASSWORD\" NAME=\"pass\" VALUE=\"\"&gt;\n&lt;INPUT TYPE=\"SUBMIT\" NAME=\"submit\" VALUE=\"Create Your Account\"&gt;\n&lt;\/FORM&gt;\n<\/span><\/pre>\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\">Now, in the response page,<span class=\"Apple-converted-space\">\u00a0<\/span><strong>my_page.php3,<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>you are handed the variables<span class=\"Apple-converted-space\">\u00a0<\/span><strong>$pass<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>and<span class=\"Apple-converted-space\">\u00a0<\/span><strong>$name.<\/strong><span class=\"Apple-converted-space\">\u00a0<\/span>You could insert the values into a table by using someting like this:<\/p>\n<pre style=\"font-family: courier,sans-serif; font-size: 10px; width: 490px; color: #000000; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; background-color: #ffffff;\"><span style=\"font-family: COURIER;\">\n&lt;?php\n\n$conn = pg_pconnect(\"host=db.company.com user=my_name \n\tpassword=my_passwd dbname=db1\");\n\nif (!$conn) {\n    echo \"An error occured.n\";\n    exit;\n}\n\n$sql=\"INSERT INTO my_table VALUES ($name, $pass);\";\n\n\/*\n\tUse the @ symbol here to suppress errors. Then use your \n\terror checking code to handle it your own way\n*\/\n\n@$result_set = pg_Exec ($conn, $sql);\n\nif (!$result_set) {\n\t\/\/No connection or other error, so print an error\n\techo \"&lt;H1&gt;ERROR - insert failed&lt;\/H1&gt;&lt;P&gt;\";\n\texit;  \/\/exit the script\n}\n?&gt;\n<\/span><\/pre>\n<p style=\"color: #000000; font-family: ARIAL, HELVETICA; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #ffffff; font-size: medium;\">No problem, right? I suggest fooling around with this code, extending it at first, and then start writing your own when you have the hang of it. Whenever I am starting a project, I basically take snippets of my code as building blocks and hack it to suit the current projects. What kind of developer writes code from scratch? \ud83d\ude0e<\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>By PHP Builder Staff on March 2, 2012 A lot of beginning developers don&#8217;t understand that the web is &#8220;Stateless&#8221;. That means that the web server forgets who you are as soon as it sends you a page. So if you&#8217;re logged in as John Doe and viewing your bank&#8230; <a href=\"https:\/\/phpbuilder.com\/getting-started-passing-variables\/\" class=\"readmore\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,12],"tags":[],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-articles","category-setup-and-installation"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=287"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":290,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/287\/revisions\/290"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}