Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP back button script 1

Status
Not open for further replies.

Sleidia

Technical User
May 4, 2001
1,284
FR
Hi guys,

I'm trying to create a back button script that can go back endlessly but it doesn't work as expected and I'm starting to lose my mind on it :(

Here is what I've done so far :

Code:
if ($_GET["ngb"] == 1) {
$dump = @array_pop($_SESSION["go_back"]["history"]);
}

$_ngb[0] = $_SESSION["go_back"]["history"];
$_ngb[0] = @array_pop($_ngb[0]);

if ($_ngb[0] == "") $_ngb[0] = $_SERVER['HTTP_REFERER'];


$script_output = "
" . $_ngb[0] . "?ngb=1
";

if ($_GET["ngb"] != 1) {
$_SESSION["go_back"]["history"][] = $current_url;
}

I think that my logic is flawed :(
What happens is that the button works only once (not very efficient ahaha)

BTW : of course I know that there already is a back button on the browser ;)

Many thanks to the one who will have some idea to share!

 
Of course you'll need to do a session_start() on each page the user visits.

Maybe something like this...

Code:
pseudo code:

on first page:
1- sess-start
2- sess var exists?
  2a - if yes
    2a.0 kill the sess var
3- create sess var
3- push this page's url into the stack
4- output page

subsequent pages:
1- sess-start

2- did user click back-button?
  2a - if yes
     2a.0 pop the array stack and redirect to the url retrieved
  2b - if no
     2b.0 push this page's url onto the stack

3- output the page

You may want to add additional logic that compensates for
the situation where the user enters a url directly into the
address bar that's already been visited.

i.e.
1- page 1 - process as above
2- user clicks a link - push onto stack
3- user clicks a link - push onto stack
4- USER enters the url of the page visited in step 2

Also, what happens when the user enters the first page directly? Do you want to still deal with the back-button?
Hmmm... And the world keeps spinning! :)
 
Thanks Darell :)

I'll try your logic flow and see if it works once translated into code.

Also, I didn't think about the case of address bar typing from the user.

Thanks again.
 

I followed your logic, the only difference being the fact that I wanted to detect the first page/other pages with the presence of the session variable.

But I still have a chicken/egg logic problem with my code ... wich means it doesn't work :(

Here it is :

Code:
session_start();

    // - ! - if first page
    if (!$_SESSION["site_public"]["nav"]["go_back"]["history"]) {
    
    // get offsite url
    $_ngb["url"] = $_SERVER['HTTP_REFERER'];
    
    // store offsite url
    $_SESSION["site_public"]["nav"]["go_back"]["history"][] = $_ngb["url"];
    
    // - ! - if other pages
    } else {
    
    // get previous url
    $_ngb["url"] = $_SESSION["site_public"]["nav"]["go_back"]["history"];
    $_ngb["url"] = @array_pop($_ngb["url"]);
    
        // - ! - if back button clicked
        if ($_GET["ngb"] == 1) {
        
        // remove last url from history
        $_ngb["dump"] = @array_pop($_SESSION["site_public"]["nav"]["go_back"]["history"]);

        // - ! - if back button NOT clicked
        } else {
        
        // get current url
        $_ngb["url"] = $current_url;
        
        // add current page to history
        $_SESSION["site_public"]["nav"]["go_back"]["history"][] = $_ngb["url"];        

        }

    }

$script_output .= "
" . $_ngb["url"] . "?ngb=1
";

Thanks :)
 
I just realized something as I really didn't think this issue through.

I don't think my logic would really work because - where does the back button actually go?

I forgot about the browser! Silly me!

A better way is actually a bit more complicated.

That is... using a router metaphor.

If you're running on Apache with mod_rewrite turned on,
you could funnel all request to one file, which performs all
of the routing to virtual pages.

i.e.
Code:
 ~ this is the .htaccess file placed in a folder
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

This tells all requests for non-static resources (files that don't actually exist) to be sent to index.php

Then you can determine which file was requested in the logic of index.php and generate the appropriate page.

The first time the user visits the page, a session is created and initialized.

Note: All of the pages that are linked to on the page that will take part in this design don't actually exist, they're generated from index.php.

The backbutton then can just be a link to something like:

You can then parse: $_SERVER['REQUEST_URI'] to see exactly what was passed to index.php and act accordingly.

I'm tired and I know I'm rambling, but this might be in
the right direction.
 
I don't think my logic would really work because - where does the back button actually go?

I forgot about the browser! Silly me!

Sorry, I don't get what you mean here :(

As for the url rewriting, I'd rather not use it.
I prefer a more simple/portable solution in the form of a small php script.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top