SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,096
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Post How to do this in php

    Hi can i ask something ,i found it here in sitepoint how to do this in php like url theres is
    private.php?do=trackpm
    when every time i click on the link it shows
    somethingpage.php?do=somevarialblehere
    when i call my page it only gives me like this in url www.mypage.com/gallery.php

    there is no
    ?do=variable
    can you help me on this how to achieve like this to have ?do=somvariable


    Thank you in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2012
    Location
    United Kingdom
    Posts
    78
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The part you're referring to is called the query string, and is created via the HTTP GET request. In your example above, the do part of the string is a key in the superglobal $_GET array, and the part to the right of the equals sign is the corresponding value.

    For example (using your query string above):
    PHP Code:
    <?php

    if(isset($_GET['do'])) {
        echo 
    $_GET['do']; //outputs "variable" (beware of XSS attacks)
    }
    One caveat to watch out for when using the HTTP GET request (especially for actions) is CSRF attacks.

  3. #3
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,096
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi, Thank you for the reply, can you please elaborate more your example, i am confuse,Thank you in advance.

  4. #4
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,096
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How do i know what page will be loaded in the variable of "do"

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you tell us what exactly are you trying to do? You can't know what will be in "do", you generate links with "do" filled out when you generate the page itself. However, note that users can place anything in there, that's why @tpunt wrote "(beware of XSS attacks)". You need to sanitize this variable if you're outputting it to the browser or do include/require based on this.

  6. #6
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,096
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi, Okay sorry for my bad english,,...i will try my best to explain you this....when I was navigating in the settings in my control panel i found out that in the left pane there is menu like my setting,my subscription etc...,...under my settings i clicked those links like edit profile,edit avatar,etc..and i found out in the url something like this
    profile.php?do=editavatar
    profile.php?do=editprofilepic
    I want to know how i can accomplish like that,if i click the link edit profile
    the url must be like this
    profile.php?do=editprofile
    and then it will load the profile form.

    just like in the sitepoint,and also it loads the form in the right side.

    I hope you got me now.

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Links:

    Code:
    <a href="index.php?do=first">First</a>
    <a href="index.php?do=second">Second</a>
    <a href="index.php?do=third">Third</a>
    Now make files first.php, second.php and third.php with different content inside. You should also make 404.php for "not found" pages. Then in index.php place following code:

    PHP Code:
    //you can place links in this file also

    if (!empty($_GET['do']))
    {
      switch(
    $_GET['do']) {
        case 
    'first':
          include 
    'first.php';
          break;
        case 
    'second':
          include 
    'second.php';
          break;
        case 
    'third':
          include 
    'third.php';
          break;
        default: 
    //default behavior, if none of above options is located in 'do'
          
    include '404.php';
      }


  8. #8
    SitePoint Wizard
    Join Date
    May 2012
    Posts
    1,096
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi thank you for the reply, Oh! so this how to do it...now i know,last thing,why did you test !empty ? instead of isset ?


    THank you in advance.

  9. #9
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    51
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's just my way of checking things, you may freely (and probably should) use isset() here. !empty() is not same as isset(), but in certain occasions it has same result. E.g. if someone opens index.php?do= it will be set, but it will also be empty... in other words in such case the isset() check would go through, but !empty() would not.

    edit: Actually, when I think about it, isset() is mandatory here. Without it you'll get unwanted notices from PHP.


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
  •