문제

I'm working on cookie-free session, I dont intend to pass variables through URL, but I try to use window.name or hidden forms. For window.name, I'd do mosething like this:

$(window).unload(function(){
    if(location.href.indexOf(<my pages base url>) != -1){
    // the user is going to my site
        if($.parseJSON(window.name).previous_site.indexOf(location.protocol + '/' + location.host) != -1){
        // now I know, that user was of my site
            cookie_alternative = new Object();
            cookie_alternative.previous_site = location.href;
            cookie_alternative.session_id = $.parseJSON(window.name).session_id; // this is important
            .....
            window.name = $.toJSON(cookie_alternative);
        }
        else{
        // the user was elsewhere
            cookie_alternative = new Object();
            cookie_alternative.previous_site = location.href;
            cookie_alternative.session_id = <something new>;
            .....
            window.name = $.toJSON(cookie_alternative);
        }
    }
    else{
    //the user goes somewhere else
        window.name = '';
    }
});

So I can track session_id this way. I suppose I'd do something similar to this, if I was to use hidden input field. And now, I want this variable (session_id) from window.name to act as if it was cookies - user clicks on some link, which takes him to different part of my web => the request is sent to the server, server responds and page is displayed. With this request I want to send session_id to server using post (as if it was a cookie)- but I can't figure out, how to do this. I was thitking about something like this:

$('a').click(function(){
    $.post({
        url: $(this).attr('href'),
        data: $.parseJSON(window.name).session_id,
    })
})

but I don't think it's going to work, and as I said, I'm not going to use URL rewriting. Other way to do this would propably be to have some hidden form type post, click on link sets form's action (href of a link), sets data: session_id and triggers form's action. Any ideas to send post to follow next pages request? Or how to use hidden form field to achieve the same thing? Any help really appreciated..

도움이 되었습니까?

해결책

$('a').click(function(e){

    //Preventing link from triggering
    e.preventDefault();     

    var elObj = $(e.currentTarget);

    $('#secret-hidden-form').attr('action',elObj.attr('href'));

    $('#secret-hidden-form').submit();
    return false;
})

At the bottom of your page:

<?php 
if (isset($_POST['session_id']) {
    $sessId = $_POST['session_id'] 
} else {
    $sessId = uniqid();
}
?>
<form id="secret-hidden-form" method="post">
    <input type="hidden" value="<?php echo $sessId; ?>">
</form>

Maybe something like this will work?

다른 팁

You can get data to the server with each page request via cookies, URL query parameters or by posting form data.

Maintaining a session across every link on your site without cookies will require a javascript override of every single URL the user visits on your site to either turn it into a form post with sessionID in the form or to add query parameters with sessionID. How practical this is depends upon how many places on your site, you have to override things.

If I were implementing a session without cookies, I'd use URL rewriting (ideally server-side) so every URL in the page either all goes through a common JS function that adds the session ID before processing the click or just rewrites the ID into the URL so the normal click can just happen and it will have the session in the URL. This allows you to preserve most of your navigation and server-side logic, but moves the sessionID from cookie to URL. Of course, this won't be persistent from one user session to the next unless the user bookmarks a URL with the sessionID in it so users will have to re-establish a session everytime they come to your site.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top