I have 2 snippets (below). The issue is the if statemnt if ($loggedin != NULL){ in the 2nd snippet doesnt pass even if the variable is not null. Its like the $loggedin variable in the 1st snippet doesnt apply to it. If I combine the 2 snippets into 1 they work fine.
Does anyone know how to make 2 snippets 'talk' to each other?
(ps, running Revo 2.1.3)
<?php
$loggedin = "";
if (!isset($_SESSION['user_id'])) {
// redirect to login page
}
else {
$loggedin = "true";
}
2nd:
<?php
if ($loggedin != NULL){
echo "logged in";
}
else {
echo "error";
}
First off, you are not passing the $loggedin variable to the second snippet, do this normally with a post or session variable.
Second, there is an easier way to check, these are straight out of Bob's guides.:
There are various methods. One easy method is to use this code:
if ($modx->user->get('username') == '(anonymous)') {
/* user is not logged in */
}
Here is the official method for seeing if the user is logged in
to the current context:
if ($modx->user->hasSessionContext($modx->context->get('key'))) {
/* user is logged in */
}
If you know the name of the current context (e.g., web),
you can use this method. The name of the context is required:
if $modx->user->isAuthenticated('web') {
/* user is logged in to web context */
}
that is if you need to roll your own authentication for some reason. ~ Otherwise, the login/register extra will do all of this for you.
*UPDATE***
Two pass variables from one snippet to another in the same resource you can set/get placeholders:
<?php
// snippet one
$modx->setPlaceholder('output','Place holder set!');
<?php
// snippet two
$myvar = $modx->getPlaceholder('output');
echo 'this is the value of "output": '.$myvar;
Related
first off all my English is not very good. Sorry I do my best. I am developing a CMS system. I need an inlog system for it. If you are not logged in you can't view some of the pages. I created a php session of it but it doesn't work on the other pages... I will include some off the code I wrote.
On the page I check the username and password, I created the session like this. And worked with session to check username and password, so it works on that page.
<?php
session_start();
$_SESSSION['gebruikersnaam'] = $_POST['gebruikersnaam'];
$_SESSSION['wachtwoord'] = $_POST['wachtwoord'];
if(($_SESSSION['gebruikersnaam'] == 'admin')
&& ($_SESSSION['wachtwoord'] == 'admin123')) {
include("adminpanel.php");
} else {
echo "Uw gebruikersnaam of wachtwoord is foutief.";
}
?>
On my other pages I added this to check if the user is logged in. I seem to never get that I am logged in and I can't echo the session out. Here is the code!
if(!empty($_SESSION['gebruikersnaam']['wachtwoord'])) {
echo "not ingelogd";
}
If the conditional statement with the session works, I can redirect the user to the log in page if he is not logged in.
Thanks in advance you would help me a lot!
That's because of a slip of the tongue, its $_SESSION not $_SESSSION and also:
$_SESSION['gebruikersnaam']['wachtwoord'];
Is actually referring to 1 value, not two:
$data = array('gebruikersnaam' => array('wachtwoord' => 'mijnwachtwoord'));
echo $data['gebruikersnaam']['wachtwoord'];
Instead do:
if(!empty($_SESSION['gebruikersnaam']) || !empty($_SESSION['wachtwoord'])){
echo "not ingelogd";
}
However, you should only store a username and id in a session. Storing the password is a potential security breach and is not necessary.
You could also use something like this to scan for required values:
function required_session_fields(array $keys){
foreach($keys as $k){
if(!array_key_exists($k, $_SESSION) && empty($_SESSION[$k])){
return false;
}
}
return true;
}
if(required_session_fields(['gebruikersnaam', 'wachtwoord'])){
echo 'gelukt';
}
If you started the session and included a file, you can still access the $_SESSION variable. On every new server request, make sure the session is started.
On the other pages you must have
session_start();
before
if(!empty($_SESSION['gebruikersnaam']['wachtwoord'])) {
echo "not ingelogd";
}
However this variable $_SESSION['gebruikersnaam']['wachtwoord'] is never created. Currently you have created $_SESSION['gebruikersnaam'] and $_SESSION['wachtwoord']
Perhaps you meant to have something like
if(!empty($_SESSION['gebruikersnaam']) && !empty($_SESSION['wachtwoord'])) {
echo "not ingelogd";
}
I have a simple login system with sessions. The user is being redirected to a page when the user succesfully logs in.
The problem is when the user leaves my site and comes back later to index.php (the same session) the user will get "Undefined index" because there's no parameter supplied when the user enter my site and is still logged in.
I use php switch to control my pages.
I have this code first in my index.php:
require_once('function.php');
session_start();
if (!is_user()) {
redirect('signin.php');
}
?>
My file with switch looks like this:
<?php
$p=$_REQUEST['p'];
if (isset($p)) {
switch ($p) {
case "vine":
include "vine.php";
break;
}
?>
Obviously $_REQUEST['p'] is undefined.
If you want your script to still know the p parameter when a user returns, you must somehow save it for further requests. This could be done like this in index.php:
<?php
session_start();
$p = isset($_REQUEST['p']) ?
$_REQUEST['p'] : (
isset($_SESSION['p']) ?
$_SESSION['p'] :
false
)
);
if ($p !== false) {
$_SESSION['p'] = $p;
switch ($p) {
case "vine": include "vine.php";
break;
}
} else {
die ('Unknown category ....');
}
?>
The code looks for an explicitely given parameter p and takes this if available. Otherwise it looks for a session parameter p.
Else it sets p to false to indicate that no value is avaible.
If a value for p is given, the session variable $_SESSION['p'] is set. And, of course, sesssion_start() must be called at the top of the script to make session variables available.
I assume the 'Invalid index' comes from $p=$_REQUEST['p'];. You want to check whether that array element exists.
if (isset($_REQUEST['p'])) {
What about this:
if (isset($_REQUEST['p']))
{
$p = $_REQUEST['p'];
// ...
}
But notice this: http://php.net/manual/en/function.array-key-exists.php#example-5520
I'm sorry to trouble you, I have tried my best to solve this but I am not sure where I am going wrong and I was wondering if someone out there would be willing to help!
Basically, I am having some issues with $_SESSION variables; I would like for each occasion that a visitor came to the page that they would be shown a different content message.. The below code, when first landing on a page will seem to skip the first "content1", and will display "content2" instead, then "content3" after another revisit. I've put in an unset call, which eventually sends it there, am I not using _SESSIONS correctly?
I'm not sure how the session variable was assigned to 1, for it to land correctly in the if===1 statement without it first returning the original "content1"
if (empty($_SESSION)) {
session_start();
}
if (!isset($_SESSION['content'])) {
$content = "content1";
$_SESSION['content'] = 1;
return $content;
}
elseif ($_SESSION['content'] === 1) {
$content = "content2";
$_SESSION['content'] = 2;
return $content;
}
elseif($_SESSION['content'] === 2) {
$content = "content3";
unset($_SESSION['content']);
return $content;
}
Apologies for babbling or whether this was a simple fix / misunderstanding on my part. It's caused quite a headache!
Many thanks.
-edit-
This is a function that is called from within the same class, it has not gone through a loop anywhere either..
You are only calling session_start(); if the session has not been created.
What about the other times, when it's 1, or 2?
Call session_start(); regardless of your if (empty($_SESSION)) { statement
You should always use the session_start() function. If a session exists, it will continue it, otherwise it will create a new session.
Your code can then be simplified to the following:
// Start/Resume session
session_start();
// Get content
function display_message()
{
if ( ! isset($_SESSION['content']))
{
$_SESSION['content'] = 1;
}
if ($_SESSION['content'] == 1)
{
++$_SESSION['content']; // Increment session value
return 'Content #1';
}
elseif ($_SESSION['content'] == 2)
{
++$_SESSION['content']; // Increment session value
return 'Content #2';
}
elseif ($_SESSION['content'] == 3)
{
unset($_SESSION['content']); // Remove session value
return 'Content #3';
}
}
// Display content
echo display_message();
However, if someone visits your page a fourth time, they will be shown the first message again (because the session value is no longer tracking what they've been shown).
Perhaps this sort of functionality might be handled better with by using a cookie to track this information?
I've got my login and session validity functions all set up and running.
What I would like to do is include this file at the beginning of every page and based on the output of this file it would either present the desired information or, if the user is not logged in simply show the login form (which is an include).
How would I go about doing this? I wouldn't mind using an IF statement to test the output of the include but I've no idea how to go about getting this input.
Currently the login/session functions return true or false based on what happens.
Thanks.
EDIT: This is some of the code used in my login/session check but I would like my main file to basically know if the included file (the code below) has returned true of false.
if ($req_method == "POST"){
$uName = mysql_real_escape_string($_POST['uName']);
$pWD = mysql_real_escape_string($_POST['pWD']);
if (login($uName, $pWD, $db) == true){
echo "true"; //Login Sucessful
return true;
} else {
echo "false";
return false;
}
} else {
if (session_check($db) == true){
return true;
} else {
return false;
}
}
You could mean
if (include 'session_check.php') { echo "yeah it included ok"; }
or
logincheck.php'
if (some condition) $session_check=true;
else $session_check=false;
someotherpage.php
include 'session_check.php';
if ($session_check) { echo "yes it's true"; }
OR you could be expecting logincheck.php to run and echo "true" in which case you're doing it wrong.
EDIT:
Yes it was the latter. You can't return something from an included file, it's procedure not a function. Do this instead and see above
if (session_check($db) == true){
$session_check=true;
} else {
$session_check=false;
}
Actually..
$session_check=session_check($db);
is enough
Depending on where you want to check this, you may need to declare global $session_check; or you could set a constant instead.
you could have an included file which sets a variable:
<?php
$allOk = true;
and check for it in you main file:
<?php
include "included.php";
if ($allOk) {
echo "go on";
} else {
echo "There's an issue";
}
Your question seems to display some confusion about how php includes work, so I'm going to explain them a little and I think that'll solve your problem.
When you include something in PHP, it is exactly like running the code on that page without an include, just like if you copied and pasted. So you can do this:
includeme.php
$hello = 'world';
main.php
include 'includeme.php';
print $hello;
and that will print 'world'.
Unlike other languages, there is also no restriction about where an include file is placed in PHP. So you can do this too:
if ($whatever = true) {
include 'includeme.php';
}
Now both of these are considered 'bad code'. The first because you are using the global scope to pass information around and the second because you are running globally scoped stuff in an include on purpose.
For 'good' code, all included files should be classes and you should create a new instance of that class and do stuff, but that is a different discussion.
I am using Joomla 2.5 and in the head of my site I have a module with the following code:
<?php
$user =JFactory::getUser()->guest;
if ($user->guest) {
echo 'Please sign up or sign in to view this'
}
else {
echo 'Here is the content';
}
?>
What I tried but didnt work is to add a variable $member in the first if statement. If the person isnt logged in to show him the echo but also set $member to == true.
Why I want to do this? I think it would make the site load faster if I check the status of $member than to go calling the Joomla API to determine if the user is logged in or not.
The calls to determine if a visitor is a member or not in every page are about 5-7. When I use the JFactory::getUser() function (all of the code above) 5-7 times it does slow down a bit the website. Thats why I thought of creating in the first call the $member == true correct me if I am wrong in thinking this would be better in site perfomance/speed wise.
I tried this:
<?php
$user =JFactory::getUser()->guest;
if ($user->guest) {
echo 'Please sign up or sign in to view this'
$member == true;
}
else {
echo 'Here is the content';
}
?>
I have the above code in the header module. But when I later in other modules in the same page or in the article if I use a php code it doesn't recognize the status of $member and always determines the user as not logged in.
What am I doing wrong? I'm guessing its something with variables scope, I checked my PHP book but couldn't find the error. Any help appreciated.
You are already calling $user->guest, so try changing:
$user = JFactory::getUser()->guest;
to this:
$user = JFactory::getUser();
As an alternative, you could also do use the following:
$user =& JFactory::getUser();
if($user->id=0){
echo 'Please sign up or sign in to view this'
$member == true;
}
else {
echo 'Here is the content';
}
You could do this in one line, Refactored code would be
$member = (JFactory::getUser()->id) ? true : false ;