An Include inside an if statement in Joomla-PHP - php

Im adding php inside an article using DirectPHP plugin.
My goal is to create a script that will include a file with text when the user has member = true; and when not to not show anything.
I have added this piece of code in a module in the top next to the logo:
<?php
if ($user =JFactory::getUser()->guest)
{
$member = false;
echo "Welcome guest, sign up and read nice quotes";
}
else
{
$member = true;
$user =& JFactory::getUser();
echo "Welcome " . $user->username;
}
?>
I have set member = true; now that the person has signed in. If he isnt signed in its on false.
Then inside the article I have:
<?php
if ($member == false)
{
$file = file_get_contents ('quotes/quotes.html');
echo $file;
}
?>
<hr id="system-readmore" />
<?php
if ($member == true)
{
include_once JPATH_SITE.'/quotes/random.php';
echo ShowQuotes();
}
?>
I cant find the problem making this not run. The quotes are shown for both $member = false; and $member = true; Are includes always being parsed despite the if statement? Same goes for file_get_contents? I tried to see if the $member declaration from the header is being kept within the parsing and wrote:
<?php
if ($member = true)
{
echo "Logged in";
}
?>
and it worked good so the problem is within the include_once and file_get_contents, I tried to pinpoint it as much as I can.
Thanks in advance for your help!

This is probably your issue:
if ($member = true)
{
echo "Logged in";
}
This is always assigning the value of true to $member.
Also here:
if ($user =JFactory::getUser()->guest)
You might have the same assignment problem (not sure if you intended to set $user and do a conditional at one here.
I might suggest getting in the habit of writing condditionals like this:
if (true === $member) { ... }
By inverting the order of the items, if you ever accidentally type = instead of == or ===, then you will get an error, instead of having the code silently perform unexpectedly.

You seem to have a scope issue here: you are declaring $member in a module, but $member won't be available to the article: and php will evaluate ($something == false) to always succeed if $something is undefined, if you want to check if a variable is really false you need to use ($something===false). Read more here: http://www.php.net/manual/en/language.types.boolean.php
That being said, since there is no real overhead to JFactory::getUser()->guest, just use that as well in you article!
btw, enable notices in your php configuration (either on screen or to a log file) and make sure you read that as you develop, it will tell you whatever is wrong, developing without seeing the errors is a guessing game, you can only bang your head against the wall so many times... you eventually need to switch on the light :-)

Related

PHP SESSION variable troubles

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?

Check result of PHP include

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.

How to check if visitor is logged in and if yes set a variable to determine further actions

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 ;

ModX: 2 snippets on same page interacting with each other

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;

What is the proper way to test CodeIgniter session variable?

Take the following code snippet. What is the best way to test to make sure the session variable isn't empty?
<?php if ($this->session->userdata('userID')) {
$loggedIn = 1;
}
else {
$loggedIn = 0;
} ?>
If later in my script, I call the following, the first prints properly, but on the second I receive Message: Undefined variable: loggedIn
<?php echo $this->session->userdata('userID'));
echo $loggedIn; ?>
I've tried using !empty and isset, but both have been unsuccessful. I also tried doing the if/then statement backwards using if (!($this->session->userdata('userID')), but no dice. Any thoughts?
Try doing the following instead:
<?php
$loggedIn = 0;
if ($this->session->userdata('userID') !== FALSE) {
$loggedIn = 1;
}
?>
If the error continues, you'll need to post more code in case you're calling that variable in another scope.
If your aim is to see whether or not the session variable 'userID' is set, then the following should work:
$this->session->userdata('userID') !== false
Why don't you create a boolean field in your session called is_logged_in and then check like:
if(false !== $this->session->userdata('is_logged_in'))
if($this->session->userdata('is_logged_in')) {
//then condition
}
This is the proper way to test!

Categories