SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    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

  2. #2
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Code:
    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.
    Code HTML:
    <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:
    PHP Code:
    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.

  3. #3
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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

  4. #4
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In "article.php" I set my page to return back to...

    PHP Code:
    // 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"...

    PHP Code:
    // 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

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    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
    Code:
    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

  7. #7
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this (escaping - so it doesn't get confused as part of a character range)
    Code:
    RewriteRule articles/([a-zA-Z0-9_\-]+)$ article.php?title=$1
    Does manually requesting articles/postage-meters-can-save-you-money work?

  8. #8
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    Try this (escaping - so it doesn't get confused as part of a character range)
    Code:
    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.


    Debbie

  9. #9
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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?

  10. #10
    Non-Member bronze trophy
    Join Date
    Nov 2009
    Location
    Keene, NH
    Posts
    3,760
    Mentioned
    23 Post(s)
    Tagged
    0 Thread(s)
    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 /.

  11. #11
    SitePoint Wizard DoubleDee's Avatar
    Join Date
    Aug 2010
    Location
    Arizona
    Posts
    3,931
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cranial-bore View Post
    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?
    What is the best way to achieve that?

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

    Code:
    <?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

  12. #12
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:


    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)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •