POST Method

Discussion in 'PHP' started by Shaimaa, Jan 31, 2010.

  1. #1
    Dear all,

    I have 3 pages which go to the same page
    example

    1. add_user.php
    2. update_user.php
    3. delete_user.php

    all these pages go to:

    show_user.php

    I want to know in the "show_user.php" page where it comes from ?

    because based on the previous page I will deal in a different way.

    I wish I find a soon replay.

    Thank You in advance.
     
    Shaimaa, Jan 31, 2010 IP
  2. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #2
    you can give your forms to different names. for example: name form "ok" for add_user.php and "ok1" to update_user.php

    on show_user.php do this:

    
    
    if (isset($_POST['ok])) 
    {
    //code for add_user.php
    }
    
    
    PHP:
    
    
    if (isset($_POST['ok1])) 
    {
    //code for update_user.php
    }
    
    
    PHP:
     
    baris22, Feb 1, 2010 IP
  3. skum

    skum Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Another alternative is to add a hidden form value to each page.

    Example:
    <input type="hidden" name="page" value="page1">

    And the just look at the value of "page"

    if($POST['page']=="page1")
    {
    //Code
    }
     
    skum, Feb 1, 2010 IP
  4. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #4
    You can do that by checking the referer, something like:
    
    if(strstr($_SERVER['HTTP_REFERER'], "add_user.php")){
    // do something for add_user.php
    } elseif(strstr($_SERVER['HTTP_REFERER'], "update_user.php")){
    // do something for update_user.php
    } elseif(strstr($_SERVER['HTTP_REFERER'], "delete_user.php")){
    //do something
    } else {
    // load default page
    }
    PHP:
     
    Bec0de, Feb 1, 2010 IP
  5. Shaimaa

    Shaimaa Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks all,

    I did something like the following

    <input type="hidden" value="add_user" name="source_file">
    
    PHP:

    switch($_POST["source_file"]){
    		case 'add_user': 
    		{// do stuff 
    			addUser();
    			break; 
    		}
    		  case 'update_user': 
    		  {
    			// do stuff 
    			updateUser();
    			break; 
    		  }
    		  case 'delete_user': 
    		  {
    			// do stuff 
    			echo "id: ".$_POST["id"]."<br>";
    			deleteUser($_POST["id"]);
    			break; 
    		  }
    	}
    PHP:
    and the problem is solved.

    Thank God :)

    Thanks all
     
    Shaimaa, Feb 2, 2010 IP
  6. Skinny Vinny

    Skinny Vinny Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    Never do this. Very bad advice.
    The HTTP_REFERER value is frequently empty. Almost every type of user privacy software/option will strip the value off the request.
     
    Skinny Vinny, Feb 2, 2010 IP
  7. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #7
    He's submitting data from an html form, the HTTP_REFERER will return the page from where the data was submitted.
     
    Bec0de, Feb 2, 2010 IP
  8. Skinny Vinny

    Skinny Vinny Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The value is sent from the browser posting the form.
    I'm not guessing here. I'm telling you from experience lol.

    Test it. Install nortons, or just turn on private browsing and see for yourself.
     
    Skinny Vinny, Feb 2, 2010 IP
  9. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #9
    Ok no problem, Test the following code in your localhost... and let me know ;)

    <form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
    <input type="submit" name="submit" value="Test!" />
    </form>
    <?php
    if (isset($_POST['submit'])){
      echo "Referer: ".$_SERVER['HTTP_REFERER'];
    }
    ?>
    PHP:
     
    Bec0de, Feb 2, 2010 IP
  10. Skinny Vinny

    Skinny Vinny Peon

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    http://forums.asp.net/p/275968/3134828.aspx#3134828
    http://codingforums.com/archive/index.php/t-7630.html
    http://www.webmasterworld.com/forum88/4108.htm
    http://karmak.org/2004/reftest/fix

    Not good enough?
    How about the php.net page on the _SERVER array? Trust that one?
    http://php.net/manual/en/reserved.variables.server.php

    I'm done arguing this with you, man.
    You use the value all you want.
    lol
     
    Last edited: Feb 2, 2010
    Skinny Vinny, Feb 2, 2010 IP
  11. Bec0de

    Bec0de Well-Known Member

    Messages:
    46
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    115
    #11
    I know that bro, But it works in most cases so Its not worthless, can be 50% trusted. :p
     
    Bec0de, Feb 2, 2010 IP
  12. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Maybe, maybe not. You can't trust HTTP_REFERER for anything. Trusting it for security will let people abuse you. Trusting it for functionality will make your application break for at least some users.

    It's advisory and nothing else. The only decent use I've seen it put to is for making more informative 404 pages.
     
    SmallPotatoes, Feb 2, 2010 IP