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

http://www.website.com/formz/test.php?do=trackpm

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.

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

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.

Hi, Thank you for the reply, can you please elaborate more your example, i am confuse,Thank you in advance.

How do i know what page will be loaded in the variable of “do”

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.

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. :slight_smile:

Links:

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


//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';
  }
}

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.

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.