2

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

5 Answers 5

6
if(isset($_GET['action']) AND $_GET['action'] == "register")
{
    // your code
}
Sign up to request clarification or add additional context in comments.

2 Comments

add an echo "<html><head><title>".$_GET['action']."</title></head><body>asdf</body></html>
@silentw Both operators can be used.
4

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 e.g. isset($_GET['page_id']).

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

Comments

1

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

Comments

0

Something like this:

$action = $_GET['action'];

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

placed in between your and tags in your php script

Comments

0

Try something like:

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

And in your HTML:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.