Confuse these cookies in the redirect system

I am working on PHP. I want to redirect the page after entering the last page I want to visit, but I am still here after 5 hours, and I still do not. This is a diagram, I have 3 php files.

newest.php (before login), 
signin.php (before login), 
thread.php (after login). 

I use cookies for this redirect. First I went to newest.php, then I clicked the button (go to thread.php). Then thread.php saw that you did not have loggin yet, and then redirected to signin.php. After I filled out the syntax form, I clicked the submit button (signin.php), then I put in signin.php (nowhere to go) even after I log in, I need to go to thread.php automatically.

this is my code in newest.php and thread.php (not in signin.php):

$coopage='coopage';
$current_page='http://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
setcookie($coopage, $current_page,time()+86400,'/');

send button to newest.php (it goes to thread.php):

echo "<center><button onclick=\"window.location='/thread/form'\">add new thread</button></center>"; 

signin.php( , , ) ( ):

if(isset($_COOKIE[$coopage])){
    $url=$_COOKIE[$coopage];
    unset($_COOKIE[$coopage]);
    header('location:'.$url);
}

note: signin.php cookie cookie, ? , 2 cookie ? cookie ( ).

$cooval2='juna';
setcookie($coousername, $cooval2, time() + (3600 * 24 * 365), "/"); // 1 year
+1
2

cookie .

1

, , login.php, $url, .

- .

<?php
session_start(); 
$_SESSION['url'] = $_SERVER['REQUEST_URI']; 

:

<?php
session_start();  // needed for sessions.
if(isset($_SESSION['url'])) 
   $url = $_SESSION['url']; // holds url for last page visited.
else 
   $url = "student_account.php"; 

header("Location: http://example.com/$url"); 

2

:

<hidden name="redirurl" value="<? echo $_SERVER['HTTP_REFERER']; ?>" />

.

, .

$_SERVER['REQUEST_URI'] . $_SERVER['HTTP_REFERER']. HTTP_REFERER , , PHP, , - , , , , -, , .

3

- $_GET.

script , , :

: $_ ['REQUEST_URI'] -

header("Location:login.php?location=" . urlencode($_SERVER['REQUEST_URI']));

, , : login.php

echo '<input type="hidden" name="location" value="';
if(isset($_GET['location'])) {
    echo htmlspecialchars($_GET['location']);
}
echo '" />';
//  Will show something like this:
//  <input type="hidden" name="location" value="previousPage.php" />

-check.php

session_start();

//  our url is now stored as $_POST['location'] (posted from login.php). If it blank, let ignore it. Otherwise, let do something with it.
$redirect = NULL;
if($_POST['location'] != '') {
    $redirect = $_POST['location'];
}

if((empty($username) OR empty($password) AND !isset($_SESSION['id_login']))) {
    $url = 'login.php?p=1';
    // if we have a redirect URL, pass it back to login.php so we don't forget it
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location: " . $url);
   exit();
}
elseif (!user_exists($username,$password) AND !isset($_SESSION['id_login'])) {
    $url = 'login.php?p=2';
    if(isset($redirect)) {
        $url .= '&location=' . urlencode($redirect);
    }
   header("Location:" . $url);
   exit();
}
elseif(isset($_SESSION['id_login'])) {
    // if login is successful and there is a redirect address, send the user directly there
    if($redirect)) {
        header("Location:". $redirect);
    } else {
        header("Location:login.php?p=3");
    }
    exit();
}
+4

cookie = > , URL-, , .

var currentPage = window.location.href;
localStorage.lastPageVisited = currentPage

, .

$(document).ready(function() {
    $('button').click(function() {
        window.location.href = localStorage.lastPageVisited;
    });
});
+1

Source: https://habr.com/ru/post/1599209/


All Articles