简体   繁体   中英

Codeigniter | Get previous URL and used as back button to previous page

I really wonder how can I do this, I have 3 pages that has the same link going to 1 page, now what I want to do is to have 1 button that is intelligently enough to get which of the 3 pages was used to go to that page and used it as it's link going back to that previous page.

Please if anyone who knows how to do this, help me to get out to this chaos. post some code how to do it in codeigniter. Thank you in advance.

通过使用一个简单的 javascript 来实现上一个按钮,例如,

<a href="javascript:window.history.go(-1);">Previous</a>

With the url helper loaded, set the current_url in a hidden field say 'refer_from'.

<input type="hidden" name="refer_from" value="<?php echo current_url(); ?>" />

Get this using Input Class and set this on the anchor link

<a href="<?php echo $this->input->post('refer_from'); ?>">Previous</a>

NOTE: using window.history.go(-1) will fail and throw a js error, if the user some how lands directly on the "1" last page. window.history will be empty in that case.

In my case jquery back methods means window.history etc,were not working so,I tried this hope this helps you guys.call this method on click. cheers

  function redirection()
        {
           <?php $send=$_SERVER['HTTP_REFERER'];?> 
            var redirect_to="<?php echo $send;?>";             
            window.location = redirect_to;

        }

In controller add:

$data['back'] = $_SERVER['HTTP_REFERER'];

In view add a button and link to $back .

//to get previous url $_SERVER['HTTP_REFERER'];

<?php $send = $_SERVER['HTTP_REFERER'];?> 
var redirect_to="<?php echo $send;?>";    

//to redirected to this link
location.assign(redirect_to);

//you can pass static link also to redirected to this link
location.assign('facebook.com');

I don't know the older version, I guess there was such funcionallity but know there is and you should avoid use the accepted answer it may break in many ways.

Know you can use method previous_url . Just add it where you need and is all done

<div class="container">
    <a  href="<?= previous_url();?>" class="btn btn-info">
        <i class="bi bi-arrow-return-left"></i>
    </a>
</div>

Returns the full URL (including segments) of the page the user was previously on.

Due to security issues of blindly trusting the HTTP_REFERER system variable, CodeIgniter will store previously visited pages in the session if it's available. This ensures that we always use a known and trusted source. If the session hasn't been loaded, or is otherwise unavailable, then a sanitized version of HTTP_REFERER will be used.

/*Back button in codeigniter project using jquery */

<button type="button" class="btn btn-primary btn-sm"  id= "back">Back</button>

//JQuery of back button

 <script type="text/javascript">
  $(document).ready(function(){
  $('#back').on('click', function(){
        <?php $send = $_SERVER['HTTP_REFERER'];?> 
        var redirect_to="<?php echo $send;?>";             
        window.location.href = redirect_to;
      });
   });
  </script>

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