Is User Logged In

Hi,
I’m currently figuring out how i can add a button/link to each of my pages to basically just redirect to /sitecake.php?scpage=currentpage,php . I would like to display this link/button only if the user is logged in. is there a variable i can use to make this happen. something like

<? if ($sitecake->User->IsLoggedID == true) ?>

Edit this page

<? end if ?>

Thanks,
Mike

Hi Mike,

Yes, you can check that with:

if ($_SESSION[‘loggedin’] == 1) { }

Hi Mike,

I made a mistake in the previous code. The right code is:

if ($_SESSION['_sf2_attributes']['loggedin'] == 1) { print "Logged in!"; }

after doing

<?php if ($_SESSION['_sf2_attributes']['loggedin'] == 1) { ?>

i get this error
Notice: Undefined index: loggedin

What you could do is check it via AJAX call on page load and render link upon getting response. When logged in sitecake should return json object {status:0} when calling sitecake//src/app.php?service=_session&action=alive. Otherwise you would get 401 Unauthorized error. Below is example using jQuery:

<script type="text/javascript"> $(function() { // Here we check if we are in edit mode or not. We don't need to render link if already in edit mode if ( !window.sitecakeGlobals || sitecakeGlobals.editMode !== true ) { $.ajax({ type: 'GET', url: 'sitecake/<version>/src/app.php?service=_session&action=alive', dataType: 'json', success: function(response) { if(typeof response !== 'undefined' && response.status === 0) { // render your link } }, error: function(xhr) { if(xhr.status === 401) { // You are not logged in so act accordingly } } }); } }); </script>

Downside of this approach is that you will need to change <version> in URL on each Sitecake update. To make it easier, you could create php include so you will need to change it in only one place when you update your sitecake.

@mrmikelu

You could prevent that error by adding if the variable is set first:

if (isset($_SESSION['_sf2_attributes']['loggedin']) AND $_SESSION['_sf2_attributes']['loggedin'] == 1) { print "logged in!"; }

I have tested this without seeing any error.

@predragleka

Would the PHP method I used cause problems? I have pages with sessions in them and I turn them off using this way.

Thanks everyone. i have incorporated all your suggestions and it did the trick

<?php if (isset($_SESSION['_sf2_attributes']['loggedin']) AND $_SESSION['_sf2_attributes']['loggedin'] == 1) { ?>
					<a id="lnkEditPage" class="button" href="/edit.php?scpage=<?php echo basename($_SERVER['PHP_SELF']); ?>" style="display:none">Edit this page</a>
					<script type="text/javascript">
						$(function() {
							if ( !window.sitecakeGlobals || sitecakeGlobals.editMode !== true ) {
								$.ajax({
									type: 'GET',
									url: 'sitecake/2.4.4/src/app.php?service=_session&action=alive',
									dataType: 'json',
									success: function(response) {
										if(typeof response !== 'undefined' && response.status === 0) {
											$('#lnkEditPage').show();
										}
									},
									error: function(xhr) {
										if(xhr.status === 403) {
											alert('not logged in');
										}
										if(xhr.status === 401) {
										// You are not logged in so act accordingly
										}
									}
								});
							}
						});
					</script>
					<?php } ?>

on my config.php file i added:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}

<?php if (isset($_SESSION['_sf2_attributes']['loggedin']) AND $_SESSION['_sf2_attributes']['loggedin'] == 1) { ?>
						Edit this page
						
					<?php } ?>

Hi,

it should work. I just suggested another way to do it. Implementation with AJAX will work on both php and plain html sites.