简体   繁体   中英

PHP Query in my URL

I am trying to write a peace of code that will change the title of the page based on what is in the url query...

?page_id=62&action=register

so

if(action='register'){
  do this
}else{
  do this
}

how would I write this? I have never dealt with this before

if(isset($_GET['action']) AND $_GET['action'] == "register")
{
    // your code
}

Access URL parameters using $_GET :

$page_id = (int)$_GET['page_id'];
$action = htmlspecialchars($_GET['action']);

if($action == 'register') {
    echo 'action: '. $action .', page_id: '. $page_id;
}

Read more about GET & POST here . If you want to check if those variables are set use isset eg isset($_GET['page_id']) .

Be careful though, it's easy to create range of vulnerabilities this way, escape/validate those variables (eg htmlspecialchars used it my code).

To read out the "action" you need the $_GET variable. You can use it with $_GET['action'] .

You should ever use isset to ask if the variable is set or not. With this you can prevent errors.

if(isset($_GET['action']) && $_GET['action'] == 'register'){ 
    do this 
}else{ 
    do this 
} 

Another example: when you ask if the action is set and after that you can ask if action == register or action == login or whatever

if(isset($_GET['action'])){ 
    switch($_GET['action']) {
        case 'register':
            //do this
            break;
        case 'login':
            //do this 
            break;
    } 
}

Something like this:

$action = $_GET['action'];

if ($action == "register") {
    echo("<title>Register</title>");
}

placed in between your and tags in your php script

Try something like:

$title = (isset($_GET['action']) && $_GET['action'] == 'register') ? 'Register' : 'Login';

And in your HTML:

<title><?=$title?></title>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM