Is it possible to hide parameters from the URL with PHP?

Asked

Viewed 1,356 times

0

I have a php project with codeigniter that gets dominio.com/?id=212454&survey=complete and after I save this data in a variable, I wanted to remove it from the url, just getting dominio.com

My current controller looks like this:

    <?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

    public function index()
    {
        $parameters = $this->input->get();
        if(isset($parameters['id'])){
            if(filter_var($parameters['id'], FILTER_VALIDATE_EMAIL) || (preg_match("/\(?\d{2}\)?\s?\d{5}\-?\d{4}/", $parameters['id']))){
                //o email ou telefone é valido
                $this->load->view('index',[
                    'survey'=>$parameters['survey'],
                ]); 
            }else{
                $this->load->view('semvariavel');
            };

        }else{
            $this->load->view('semvariavel');
        };

    }
}

I am using the following solution in front-end:

    <script>    
    if(typeof window.history.pushState == 'function') {
        window.history.pushState({}, "Hide", '<?php echo $_SERVER['PHP_SELF'];?>');
    }
</script>
  • 1

    Which web server are you using? Apache?

  • Pass the data by post instead of get

  • @bfavaretto yes

  • @Wotonsampaio is not possible, it will be a link type ref, the person already enters with the parameters

  • See if the content on the page I indicated as duplicate solves your problem. If not, let me know here.

  • @bfavaretto tried not to work

  • It’s kind of weird what you describe as wanting to do...

  • is a search page, I do not want the person to see her ref after entering the page

  • Maybe a PHP redirect, then?

  • 1

    The two possibilities are, 1. after the page load run history.pushState - 2. use Ajax and not page - Extra: of course you can also choose to use friendly Urls, which technically would still be different urls, may be what you are looking for may not.

  • It seems that the solution is just front end, the problem is that by doing this on the front, I’m getting url.com/index.php, but I think I can solve it with . htaccess

Show 6 more comments

1 answer

1


You cannot make changes to the browser via PHP, because PHP is processed on the server, not the client (browser).

What you can do is run a javascript script in this view that does this, since javascript yes runs on the client side.

One way to do this is with the command history.pushState, but only in browsers that has support for this action. Example:

history.pushState({},"URL Rewrite Example","https://stackoverflow.com/example")

You can learn more about the pushState() method in: Mozilla Developer

Browser other questions tagged

You are not signed in. Login or sign up in order to post.