Chapter 12 - Review

Output
Show all Hide all
1Toggle answer visibilityWhat code is used to redirect the user's browser from one page to the next?

Using PHP, a browser redirect can be accomplished by calling

header('Location: www.some.absolute/url.com');
As with all header modifications, it must be called before any content is output to the browser!
Depending on your goals, you could also modify the folder's .htaccess file by adding
Redirect /from.url http://to.url

2Toggle answer visibilityWhat does the headers already sent error message mean?

If you receive the error Headers already sent you need to double check your scripts/code for any output that would have been sent to the server before your header request.

3Toggle answer visibilityWhat value does $_SERVER['HTTP_HOST'] store? What value does $_SERVER['PHP_SELF'] store?

$_SERVER['HTTP_HOST'] contains the contents of the Host: header from the current request, if there is one. *compare with SERVER_NAME.

$_SERVER['PHP_SELF'] contains the file name and path to that file. It does NOT contain the query string if present.

url = http://www.test.com/dir1/dir2/filename.php?name=john
HTTP_HOST == www.test.com
PHP_SELF == /dir1/dir2/filename.php

4Toggle answer visibility What does the dirname() function do?

Given a string containing the path of a file or directory, dirname() will return the parent directory's path.

$string = home/user1/dir1/dir2/filename.php dirname($string) = home/user1/dir1/dir2

$string = http://www.test.com/dir1/dir2/filename.php dirname($string) = /dir1/dir2

5Toggle answer visibilityWhat does the rtrim() function do? What arguments can it take?

The rtrim() function will remove any white space or specified characters from the end (rightside) of a string.

		$string = "text.html";
		$newString = rtrim($string,'.html'); //$newString = 'tex'
		

6Toggle answer visibilityHow do you write a function that returns multiple values? How do you call such a function?

To return multiple values from a function, you must insert them into an array and return the array. When calling a function that returns an array of values, use the list() function to assign each array item to a variable. (list() only works with numeric arrays!)

		list($val1,$val2,,$val4) = get_array_values();
		

7Toggle answer visibilityWhat arguments can the setcookie() function take?

setcookie($name,$value,$expire,$path,$domain,$secure,$httponly)
Name is the only required parameter to create a cookie.
Value is the stored value accessible through $_COOKIE['name']. The value is automatically urlencoded when you send the cookie, and when it is received, it is automatically decoded.
Expire is a numeric Unix timestamp of the date the cookie will expire from the client - if 0 cookie will expire at end of current session.
Path is the directory through which this cookie is available. If set to '/' - cookie is available on whole domain.
Domain indicates the domain and subdomains that the cookie is available to.
Secure indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
HTTPOnly when TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won't be accessible by scripting languages, such as JavaScript.

8Toggle answer visibilityHow do you reference values previously stored in a cookie?

To access a stored cookie use

$value = $_COOKIE['name'];

9Toggle answer visibilityHow do you delete an existing cookie?

To delete an existing cookie - simply call setcookie() with the same parameters it was created but setting value to a Null string and expires to the past.

10Toggle answer visibilityAre cookies available immediately after being sent (on the same page)? Why can you still refer to a cookie (on the same page) after it is deleted?

Cookies are not available immediately after being sent. Because they are sent with the HTTP headers, they require a reload of the page in order to become useful. This is also why the cookie value is still available on the same page that it is deleted.

11Toggle answer visibility What debugging steps can you take when you have problems with cookies?

If cookies are not behaving you can do several things. Check that cookies are not disabled in your browser. Turn on Firebug (in Firefox) and use the development panel to monitor cookies created and destroyed. PrintR the $_COOKIE superglobal to see it's contents. Make sure the size of your cookie doesn't exceed browser limitations and also that you are not trying to save too many cookies! Make sure you have not sent any output to the browser before your setcookie request. Make sure you are not accessing the cookie value before it is available to your script.

12Toggle answer visibilityWhat does the basename() function do?

The basename($path, suffix) function returns the trailing name component of a given path with an optional suffix removed from the end.

		$path = http://www.path/to/file/name.html
		$file = basename($path,'.html'); // $file = name

13Toggle answer visibilityHow do you begin a session?

To begin a new session or access an already existing session use session_start().

14Toggle answer visibilityHow do you reference values previously stored in a session?

Use the $_SESSION['name'] to access previously stored values. Make sure you have called session_start() before hand.

15Toggle answer visibilityIs session data available immediately after being assigned (on the same page)?

Unlike cookies, session data is immediately available to the same script directly after being assigned.

16Toggle answer visibilityHow do you terminate a session?

To end a session you need to unset any $_SESSION variables stored on the server, delete the session cookie from the users browser and destroy the actual session.

17Toggle answer visibilityWhat debugging steps can you take when you have problems with sessions?

If sessions are not behaving - use firebug to view the session cookie value or print out the session id to see if it is the same value throughout your script. If it changes - your code is making multiple sessions with different saved values. Make sure you are properly calling session_start() before setting or accessing any session values. Find out if cookies are disabled in the browser.

Source
<?php 
$review = array(
	1 => array(
		'q' => 'What code is used to redirect the user\'s browser from one page to the next?',
		'a' => '<p>Using PHP, a browser redirect can be accomplished by calling <pre>header(\'Location: www.some.absolute/url.com\');</pre>As with all header modifications, it must 
		be called before any content is output to the browser! <br/>Depending on your goals, you could also modify the folder\'s .htaccess file by adding
		<pre>Redirect /from.url http://to.url</pre></p>'
	),
	2 => array(
		'q' => 'What does the headers already sent error message mean?',
		'a' => '<p>If you receive the error <b>Headers already sent</b> you need to double check your scripts/code for any output
		that would have been sent to the server before your header request.</p>'
	),
	3 => array(
		'q' => 'What value does $_SERVER[\'HTTP_HOST\'] store? What value does $_SERVER[\'PHP_SELF\'] store?',
		'a' => '<p><b>$_SERVER[\'HTTP_HOST\']</b> contains the contents of the Host: header from the current request, if there is one.
		*compare with SERVER_NAME.</p><p><b>$_SERVER[\'PHP_SELF\']
		</b> contains the file name and path to that file. It does NOT contain the query string if present.</p><p>
		 url = http://www.test.com/dir1/dir2/filename.php?name=john <br />HTTP_HOST == www.test.com<br/>PHP_SELF ==  /dir1/dir2/filename.php</p>'
	),
	4 => array(
		'q' => ' What does the dirname() function do?',
		'a' => '<p>Given a string containing the path of a file or directory, <b>dirname()</b> will return the parent directory\'s path.</p>
		<p>$string = home/user1/dir1/dir2/filename.php   dirname($string) = home/user1/dir1/dir2</p>
		<p>$string = http://www.test.com/dir1/dir2/filename.php    dirname($string) = /dir1/dir2</p>'
	),
	5 => array(
		'q' => 'What does the rtrim() function do? What arguments can it take?',
		'a' => '<p>The rtrim() function will remove any white space or specified characters from the end (rightside) of a string.
		<pre>
		$string = "text.html";
		$newString = rtrim($string,\'.html\'); //$newString = \'tex\'
		</pre></p>'
	),
	6 => array(
		'q' => 'How do you write a function that returns multiple values? How do you call such a function?',
		'a' => '<p>To return multiple values from a function, you must insert them into an array and return the array.
		When calling a function that returns an array of values, use the list() function to assign each array item to a variable.
		(list() only works with numeric arrays!)
		<pre>
		list($val1,$val2,,$val4) = get_array_values();
		</pre></p>'
	),
	7 => array(
		'q' => 'What arguments can the setcookie() function take?',
		'a' => '<p><b>setcookie($name,$value,$expire,$path,$domain,$secure,$httponly)</b><br/>
		Name is the only required parameter to create a cookie.<br />
		Value is the stored value accessible through $_COOKIE[\'name\']. The value is automatically urlencoded when you send the cookie, and when it is received, it is automatically decoded.<br />
		Expire is a numeric Unix timestamp of the date the cookie will expire from the client - if 0 cookie will expire at end of 
		current session.<br />
		Path is the directory through which this cookie is available. If set to \'/\' - cookie is available on whole domain.<br />
		Domain indicates the domain and subdomains that the cookie is available to.<br />
		Secure indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.<br />
		HTTPOnly when TRUE the cookie will be made accessible only through the HTTP protocol. This means that the cookie won\'t be accessible by scripting languages, such as JavaScript.
		</p>'
	),
	8 => array(
		'q' => 'How do you reference values previously stored in a cookie?',
		'a' => '<p>To access a stored cookie use<pre>$value = $_COOKIE[\'name\'];</pre></p>'
	),
	9 => array(
		'q' => 'How do you delete an existing cookie?',
		'a' => '<p>To delete an existing cookie - simply call setcookie() with the same parameters it was created but setting value to a Null string and expires to the past.</p>'
	),
	10 => array(
		'q' => 'Are cookies available immediately after being sent (on the same page)? Why can you still refer 
		to a cookie (on the same page) after it is deleted?',
		'a' => '<p>Cookies are not available immediately after being sent. Because they are sent with the HTTP headers, they require a reload of the page in order to become useful.
		This is also why the cookie value is still available on the same page that it is deleted.</p>'
	),
	11 => array(
		'q' => ' What debugging steps can you take when you have problems with cookies?',
		'a' => '<p>If cookies are not behaving you can do several things. Check that cookies are not disabled in your browser. Turn on
		Firebug (in Firefox) and use the development panel to monitor cookies created and destroyed. PrintR the $_COOKIE superglobal to see 
		it\'s contents. Make sure the size of your cookie doesn\'t exceed browser limitations and also that you are not trying to 
		save too many cookies! Make sure you have not sent any output to the browser before your setcookie request. Make sure you are not
		accessing the cookie value before it is available to your script.</p>'
	),
	12 => array(
		'q' => 'What does the basename() function do?',
		'a' => '<p>The basename($path, suffix) function returns the trailing name component of a given path with an optional suffix removed from the end. 
		<pre>
		$path = http://www.path/to/file/name.html
		$file = basename($path,\'.html\'); // $file = name</pre></p>'
	),
	13 => array(
		'q' => 'How do you begin a session?',
		'a' => '<p>To begin a new session or access an already existing session use <b>session_start()</b>.</p>'
	),
	14 => array(
		'q' => 'How do you reference values previously stored in a session?',
		'a' => '<p>Use the $_SESSION[\'name\'] to access previously stored values. Make sure you have called session_start() before hand.</p>'
	),
	15 => array(
		'q' => 'Is session data available immediately after being assigned (on the same page)?',
		'a' => '<p>Unlike cookies, session data is immediately available to the same script directly after being assigned.</p>'
	),
	16 => array(
		'q' => 'How do you terminate a session?',
		'a' => '<p>To end a session you need to unset any $_SESSION variables stored on the server, delete the session cookie from the users browser and
		destroy the actual session.</p>'
	),
	17 => array(
		'q' => 'What debugging steps can you take when you have problems with sessions?',
		'a' => '<p>If sessions are not behaving - use firebug to view the session cookie value or print out the session id to see if it is the same value throughout 
		your script. If it changes - your code is making multiple sessions with different saved values.
		 Make sure you are properly calling session_start() before setting or accessing any session values. Find out if cookies
		are disabled in the browser.</p>'
	)
);
include('templates/review.php');