php web design software open source
[php web development] [tommy macwilliam] [this is cs50.] [cs50.tv] >> in this video, we'll learn about using php for web development. php is a scripting language that can be used to implement websites on a web server. a web server is essentially a machine dedicated to providing content that can be accessed via the internet. when you navigate to a web page like facebook.com/home.php
the code in the file called home.php which lives on a facebook web server somewhere will be executed on that server. this code will likely generate some output which will in turn be sent from the server to your web browser. we'll be using the cs50 appliance as a web server. your machine probably won't be nearly as powerful as the machines in a facebook data center, but you'll have no problem using it for web development. >> when we navigate to a url like http://localhost/hello.php
we configure the appliance via an application called the apache http server to look for a file called hello.php inside of home/jharvard/vhosts/localhosts/html by default. if that file exists then apache will use the php interpreter to execute the php code in hello.php. if that file doesn't exist then apache will throw a not found error or a 404 error, which you've probably seen while browsing the web. >> let's take a look at hello.php. we can see here that hello.php generates a signal line of output. when we ran hello.php at the command line via php hello.php
that output was printed to the terminal. now, when we access this file via a url in the web browser its output will be sent to the web browser, so heading to the url, http://localhost/hello.php, we can see the output in our web browser. >> let's try adding another printf to our hello world program. okay, let's head back to the web browser and see what we have. interesting. rather than printing another line on its own line, as you would have seen
in the terminal, it looks like it got smushed onto the same line as the other printf, so maybe new lines don't work in php. not quite. remember that html is typically used to create web pages that can be displayed by web browsers. not only is the string hello from php not valid html, but recall that we cannot use the \n character in html to create a line break. instead of simply outputting a string let's output valid html.
by using paragraph tags each of our printf calls will be displayed on its own line, so now when we visit the url pointing to valid.php http://localhost/valid.php we see the output that we're looking for. >> now, if we view the source of this page we can see that we're now looking at valid html, which we created from php. putting all of our html inside printf calls
is of course going to get really annoying. luckily we can easily mix html and php in the same .php file. remember, all of our php code must be enclosed within . anything that is not enclosed within these delimiters will simply be sent as output to the browser rather than being executed. that means we can do something like this.
we can simply write html inside of our .php file and then insert php blocks wherever we'd like some php code to be executed. here we define a few variables at the top of the file, and later we print them out inside of our html. now if we visit this url, http://localhost/mixed.php we can see our evaluated php inside of our html. >> now let's take a look at how we can pass data among our various php pages. rather than saying
we can simply say
while ampersands separate pairs. a url that looks like http://localhost/get.php?foo=bar&baz=qux has 2 key value pairs in the query string. the key foo maps to the value bar, and the key baz maps to the value qux. we can easily access these key value pairs using a special variable in php, $_get. $_get is an associative array that is automatically populated with query string data.
that means that given this url $_get["foo"] will be equal to the string bar. >> let's take a look at get.php to see $_get in action. here we're using a function called var_dump, which when given an array or other variable will print it out for us. now if we simply access http://localhost/get.php then we'll see an empty array because we haven't provided a query string. if we do provide a query string via http://localhost/get.php?foo=bar&baz=qux
then we can see that the $_get variable will contain the query string's key value pairs. but what if we don't want to put our data inside the url of a page? for large amounts of data, this can result in some pretty ugly urls that are going to make our shiny website look lame. we can instead put the query string into the body of the http requests rather than the request's url. then we can use php's $_post variable to access the key value pairs.
one way to do this is via an html form. here we have a simple html form. notice here that the method attribute of this form is post. this tells the browser to put the form's key value pairs into the body of the request rather than the url. >> if we were to use the value get for this attribute then the form's key value pairs would instead go into the query string, so we could access them via $_get again. the action attribute of the form tells the browser where to send the data.
here our 2 input elements have name attributes. the value of the name attributes will serve as keys into our data, and the values of the text inputs will become values of those keys. now let's take a look at post.php, the file that this form is submitting to. just like we did before, we're simply displaying the contents of the $_post variable. >> let's navigate to the form with http://localhost/form.php. now when we submit the form we can see that the data from form.php is passed to post.php without appending a query string to the url.
now we've seen 2 different ways of passing data between php pages, get and post. in our examples, we used 2 different types of http requests. as you might expect, a get request was used when we populated $_get from the url, and a post request was used when we populated $_post. in designing your web apps it's a good rule of thumb to use get requests when your app will only read data, and post requests when your app will write data.
for example, a search query will read data from your app, so a get request makes sense. on the other hand, your app will write data via something like a registration form, so a post request would make more sense, and that's an overview of some of the techniques we'll be using in cs50 to create websites using php. >> my name is tommy, and this is cs50. [cs50.tv]
Tidak ada komentar:
Posting Komentar