0

Case: User clicks on an image

  • If it is a returning user (user session valid for 30 minutes) navigate to the link of the image
  • If it is a new user show a pop-up div

I am now showing the pop-up div whenever the image is being clicked. Any advice on how to save the session and refer it as the variable in a JavaScript to perform a function like:

if {
    // user is log in.. do your task here..
} else {
    //user is not log in.. do your task here...
}

EDIT:

<script>
<? if(isset($_SESSION["logged_in"])) {
    echo "var $check=1";
} else {
    echo "var $check=0";
} ?>

function SetMood() {
    if($check==1) {
        // user is log in.. do your task here..
        alert('URL');
    } else {
        //user is not log in.. do your task here...
        document.getElementById('light').style.display='block';
        document.getElementById('fade').style.display='block';
        document.getElementById('fade').scrollIntoView(true);
    }
}
</script>

This is called for the image as:

<a id="register" href = "javascript:void(0)" onclick = "SetMood();" data-fancybox-group="gallery">
    <img src="<cms:show my_image_thumb />" alt="" class="fade">
</a>

The above has no effect and now nothing opens onClick

3
  • i guess you can write the javascript automaticly via php and insert the value of the session.. Commented Feb 10, 2015 at 13:54
  • How exactly would I save the session in the JavaScript? Please see edit to the question Commented Feb 10, 2015 at 13:56
  • People have been using cookies to do this for a generation. Commented Feb 10, 2015 at 13:56

2 Answers 2

2

Don't perform the conditional check in client-side code, perform it in server-side code.

Assuming you have JavaScript implementations of what you want to achieve, let's call them these respectively:

navigateToLink();
showPopUp();

Then you'd conditionally emit the code for one or the other depending on a server-side session check. Something like this:

<?php
if ($_SESSION['someValue'] == 'someKnownValue') {
?>
    navigateToLink();
<?php
} else {
?>
    showPopUp();
<?php
}
?>

The idea is that server-side code should determine what the user can or can not do. Anything emitted to client-side code is entirely visible to (and modifiable by) the user.

Sign up to request clarification or add additional context in comments.

13 Comments

How would I make a call to the php script in the onClick for the image? Since php only responds to get, post etc, will I be setting the value to true?
You can't. If you don't have the functions showPopup() and navigateToLink(), you still have to write them.
I already have the functions showPopup() and navigateToLink() but I need to make a call to the PHP on image click to show the correct function
@Sarah: If you want to invoke server-side logic without a page refresh then that would be an AJAX request. You can have a server-side resource ("page" in PHP, most likely) which responds with data instead of markup, and the client-side code would consume that data.
I think you mean an ajax call, you want to call a php script using javascript?
|
0

This should help you:

<script>
    var yourValue = '<?php echo $_SESSION['yourSessionValue'] ?>';
    if(yourValue == 'hello') {
        console.log('hide yo kids');
    } else {
        console.log('hide yo wife');
    }
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.