A question mark in the URL that a header function uses

There is a file named cart.html.php on page 277 of the fifth edition of PHP & MYSQL: NOVICE TO NINJA. I’m assuming the header function on page 279 uses the name of that file as a part of the URL (?cart). Specifically, the code is:

header(‘Location: ?cart’);

What does the question mark do?

The part of a URL that follows a question mark is a query string, which usually consists of parameters with assigned values.
Though in this case there is only a parameter name (cart) with no vaule.
A value will follow an = after the paramter name.
So you could have ?id=123 where the value of id is 123.
In PHP you can access these parameters and values via the $_GET array.

Could it be that the .htaccess file is translating that in some way, as well as the method @SamA74 describes?

Which version of the book is it? I have the 6th edition (latest) and don’t see that on page 277.



Thank you.

I could only find anything about an .htaccess file on page 188 in the edition I have, which is the fifth edition. I couldn’t tell if this type of file is only used with Apache servers.

Thank you.

The file named cart.html.php on page 277 contains:

<input type="submit" name="action" value="Empty cart">

The code on page 279 (in index.php) contains:

if (isset($_POST['action']) and $_POST['action'] == 'Empty cart')
  // Empty the $_SESSION['cart'] array
  header('Location: ?cart');

Please let me know if I should paste more code, or delete code because of copyright reasons.

How can cart (which was mentioned above) be useful to the header function without anything in the URL before the ? or anything in the URL after the t?

Hi jbengia1 welcome to the forum

What are the <form … > attribute values?

I know that Apache does use it, and that the web server I’m more familiar with does not, but I don’t know whether other servers like IIS have an equivalent. What server are you using?

Having nothing before the ? will normally open the default home, and having nothing after the t could be a rewrite rule as I said above. I’m not sure whether just having the parameter specified, without a value, would allow it to be used in an isset() check in the destination page, too.

I don’t have the book so I can’t say what might or might not be happening.

It will be return true from isset without a value.
A var_dump of $_GET['cart'] gives an empty string.

I’ve not read that version of the book, but I think it is used like that: if(isset($_GET['cart'])) { // Do something } else { // Do something else }

I think I know why you asked that question, because of the value of the action attribute, which I think is in the URL used by the header function. Here is the code:

    <form action="?" method="post">
        <a href="?">Continue shopping</a> or
        <input type="submit" name="action" value="Empty cart">

That is wrong, isn’t it?

1 Like

We had this discussion recently, which may be of assistance.

As explained, ?cart will not take you a file called cart.
The absence of anything before the ? means the file accessed will be index.php (or whatever the homepage is called), but with the query string cart appended to the URL, which will most likely be used in a condition like I posted.
The action="?" will make the form use the same URL as the form itself, but the absence of anything after the ? will remove any query string appended to the current form URL.

What I beleive this does is to allow a single controller file have a number of different outputs (such as display apparently different web pages) dependant on the query string, or lack of.

to be accurate: the file accessed will be the current file, whatever that file is.


That’s right, the location will be relative to the current page.

Thank you for comparing ? and “”. On page 127, it says that header('Location: .'); will also point to index.php. Would you please compare . with those two (? and “”)?

I think it’s correct to say that "." is shorthand for “the current directory”, in a similar way that ".." is shorthand for “the next directory level up”, i.e. the parent directory.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.