Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Adding a Basic Form Redirection

3 Answers

andrewgabriel
andrewgabriel
18,105 Points

$POST variables are used in order to encrypt data from the front-end user to see therefore when a page is refreshed the values are not sent to the new page. In most common cases, the POST values are going to a database or a file to check something or extract data based on these values entered.

On the other hand $GET variables server the same purpose however they DO get sent to the refreshed page and if you want to try this in action, try setting up a form with method set to get then fill out the form and submit it. You will see on the refreshed page in the URL things like ?name=andrew?email=a@a.com etc... and that's the main the different.

I hope that was a clear enough explanation, if you want to read more about this, I suggest you take a look at https://www.w3schools.com/tags/ref_httpmethods.asp

Yu-Che Hung
Yu-Che Hung
Courses Plus Student 10,607 Points

What does the following code return?

<?php

echo "Hello World!";

header("Location: newfile.php");

Yu-Che Hung
Yu-Che Hung
Courses Plus Student 10,607 Points

hi andrew : I forgot to paste the question THAT I don't know

andrewgabriel
andrewgabriel
18,105 Points

Yen Wei Liu The code you posted will print Hello World! to the screen then immediately go to the page named newfile.php.

Using the header("Location: anyfilename.php"); is a very powerful function in PHP that is used to redirect users from one page to another. To put it in a practical example, you might be developing a login page where users enter some values in the email and password field then after they hit submit and you verify their information, you will need to use this function to redirect them to their profile page or the homepage inside your web application.

It can also be used for other instances like redirecting users to a thank you page after they fill out and submit a contact form.

Yu-Che Hung
Yu-Che Hung
Courses Plus Student 10,607 Points

But the answer is A: Hello World! and a Warning that's why I don't get it. Originally, I was thinking about the same as you posted. But the answer just made me confused.

andrewgabriel
andrewgabriel
18,105 Points

Yu-Che Hung I see what you mean now. I just tested the code through http://phptester.net and there is indeed a warning that prints after Hello World!

After doing some research I found out that you can't redirect the page after already printing something to the user or in other words sending a response. I recommend you check out this thread on Stackoverflow, it goes in more detail about other ways you can make this work. http://stackoverflow.com/questions/43171753/how-to-redirect-to-another-page-in-php-using-header-after-outputting-message

Great question btw!

Yu-Che Hung
PLUS
Yu-Che Hung
Courses Plus Student 10,607 Points

thanks! you're so patient with my questions! appreciate for that!