About GET, POST, etc

I think I am confused about GETs, POSTs, and Redirects.

What causes a GET to come into existence?

What causes a POST to come into existence?

When you “Redirect”, what is that?

Debbie

GET and POST are types of HTTP requests. The client (e.g. browser) will send the request to the server which can include GET/POST values

GET will come from a query string in the URL.


http://example.com/script.php?something=value

$_GET[‘something’] will be ‘value’;

POST occurs most often when the user has submitted a form, and it had a method of POST. The URL won’t show a change as with GET, but the data will be sent as part of the request.


<form method="post" action="script.php">
<input type='text' name='something' value='value'>
</form>

$_POST[‘something’] will be ‘value’;

POST can be sent without a form.

There are different types of redirect. Typically the server will send a HTTP header to the user agent which has a redirect instruction, or a status code indicating the resource has moved.

Examples:


header("Location: http://example.com/somewhere-else.html");

Browsers will redirect in this case, but if you depend on your script end you must also call exit() because you can’t trust them to redirect. It’s just an instruction.

There are also HTML meta redirects and JavaScript ones, but they’re not so relevant.

Why is it that when I did a redirect with a “pretty URL” that there wasn’t a GET involve?

My “pretty URL” is supposed to be converted to a Query String and so based on what you said I’d expect GET to have a value…

Debbie

In “article.php” I set my page to return back to…


// Set Article Title.
$articleTitle = $_GET['title'];
$_SESSION['articleTitle'] = $_GET['title'];

// Set current Script Name + Query String.
$_SESSION['returnToPage'] = $_SERVER['SCRIPT_NAME'] . '/' . $articleTitle;

And then after the user successfully logs in on “log_in2.php” my code is supposed to bring them back to the specific article they were reading on “article.php”…


// Redirect User.
if (isset($_SESSION['returnToPage'])){
	header("Location: " . $_SESSION['returnToPage']);
}else{
	// Take user to Home Page.
	header("Location: " . WEB_ROOT . "index.php");
}

// End script.
exit();

But if I have a returnToPath = '/article.php/postage-meters-save-you-money then why isn’t it working and creating a GET value?

Debbie

Do you have a rewrite rule (.htaccess) to convert article.php/title into article.php?title=$title
If not, then having article.php/something won’t create a GET variable. You need the ? in there.

.htaccess


RewriteEngine on

#PRETTY:		articles/postage-meters-can-save-you-money
#UGLY:		article.php?title=postage-meters-can-save-you-money

RewriteRule articles/([a-zA-Z0-9_-]+)$ article.php?title=$1

Debbie

Try this (escaping - so it doesn’t get confused as part of a character range)


RewriteRule articles/([a-zA-Z0-9_\\-]+)$ article.php?title=$1

Does manually requesting articles/postage-meters-can-save-you-money work?

My mod_rewrite works just fine on its own.

Redirecting from “log_in2.php” back to “/articles/postage-meters-can-save-you-money” is the issue. :frowning:

Debbie

The header function is supposed to get a full absolute URL, but normally browsers can cope with a relative one. Have to echoed $_SESSION[‘returnToPage’] to see that the value is what you expect?

Starting to be reminded of why I redirect EVERYTHING that isn’t a static file at a single index.php, then manually parse $_SERVER[‘REQUEST_URI’] instead.

Which would be my advice on it. Pathinfo, then explode on /.

What is the best way to achieve that?

Sounds like a need for my config.inc.php file which goes like…


<?php
	define('ENVIRONMENT', 'development');
	//define('ENVIRONMENT', 'production');

	// File Root
	define('ROOT', ENVIRONMENT === 'development'
			? '/Users/user1/Documents/DEV/++htdocs/01_MyProject/'
			: '/var/www/vhosts/mywebsite.com/httpdocs/');

	// Web Server Root
	define('WEB_ROOT', ENVIRONMENT === 'development'
					? 'http://local.dev/'
					: 'http://www.mywebsite.com/');
?>

Would that work?

Debbie

[OT]
Slight note - there is an HTTP GET, and PHP’s $_GET. HTTP GET is a HTTP Verb (Protocol Method), used to describe the transaction desired with a message sent to a web server. GET is used to tell the webserver “I want the following resource.”. POST is likewise, but specifies to the server that there is data being passed.

A Redirect is an HTML construct as well - in response to the HTTP GET/POST, the server (whether prompted by PHP, or apache, or whatever) responds with one of a couple of status numbers that indicate the URL has been redirected (Any status code in the 300 range is classified as a Redirect)

PHP’s $_GET and $_POST are superglobal variables that always ‘exist’, but are empty unless there are variables passed in the query-string section of a URL (for $_GET), or in the message section of a POST operation ($_POST). It is perfectly possible to pass both at the same time (Send a POST operation, aimed at a URL the contains query string variables)[/OT]