How to close brakets in php $_SESSION? - php

when i use first code it works but not working for second one.
first code that works fine
<?php
session_start();
if(!isset($_SESSION['user_email'])){
//if not logged in
header ("location: login.php");
}
else {
?>
welcome page
<?php } >
But when i try to do this only "if" statement is working "else" is showing nothing
<?PHP
session_start();
if(!isset($_SESSION['user_email'])){
?>
html codes here (works fine )
<?php}
else { ?>
diffrent html codes here
(but this section is not working)
<?php }?>
what is wrong and how should i close "{} " in this case ...please help

Hi Jannatul!
In your second PHP opening tag, you have this:
<?php}
This without a space may be causing issues while parsing your code. Try and replacing it to this:
<?php }
If this works please tell me so, and if it doesn't I will gladly be willing to assist you further.
Matt

Try this without that mess..
<?php
session_start();
if(!isset($_SESSION['user_email']))
{
echo 'html codes here (works fine )';
}
else
{
echo 'diffrent html codes here (but this section is not working) - but maybe now does';
}
?>

Related

Order of inclusion and interpretation for php and html

I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.

$string is not showing in common.php

Let's say I've got 2 files. 1 is common which loads all the design and stuff and one is index.
What I want to do is set a $ in index like this:
<?
$SubId3 = 'test';
include "../../common.php";
?>
Then in common I want to have something like
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
I cannot seem to get this working. Meaning if I set it up this way. The index will never show "test".
What am i doing wrong here?
I want to do this since only certain files will contain the string $SubId3, to test some things on certain pages and not others (by adding $SubId3 = 'test'; to that particular file)
Note that <?= is short-hand to output something (think of <?= as <?php echo) and not to execute any other sort of logic or code.
However, it is possible to use the ternary operator this way:
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
This is basically equivalent to this:
<?php
if (empty($SubId3)) {
echo 'homepage';
}
else {
echo $SubId3;
}
?>
So the <?= short-hand should only be used to pass one simple variable or a ternary expression to it; everything else should use the common <?php tag.
Here's a test case for Alex (in the comments) because I can run the above code just fine with PHP 5.4.12, but he seems not to be able to.
common.php
<?= empty($SubId3) ? 'homepage' : $SubId3; ?>
index.php (visit this file then)
<?php
$SubId3 = 'test'; // <-- Comment this out for the "homepage" output
include 'common.php';
i think this
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
should be
<?php $SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
<?=?> is short for <?php echo?>
This wont work:
<?=$SubId3; if (empty($SubId3)) { echo 'homepage'; } ?>
If you want to print some stuff, you have to use only the variable, in one block and the IF on another.
<?=$SubId3?>
And:
<?php if(empty($SubId3)) { echo 'homepage'; } ?>
Hope this helps...
Try
<?php
/* echo $SubId3; */
if (empty($SubId3)) {
echo 'homepage';
} else {
echo $SubId3;
}
?>
Consider using different style of coding.
In PHP you have generally three variants:
PHP code only
HTML files with just some echoes
Intermixed PHP and HTML
In first you use echo to output every single bit of the HTML.
Second means you include a PHP script at the top of your HTML file and call appropriate functions / insert text into the template. Just so you can edit your HTML separately from your PHP.
Third makes for sometimes unreadable and complex code, but is fast to write.
<?php if($something) {
while($otherthing) { ?>
<B>text=<?=$index ?></B>
<?php }} ?>
Just a food for thought.
I found the answer guys, thanks for all the help.
I needed to set it in the PrintHeader like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
And the index had to look like this:
<?
include "../../common.php";
printHeader('BlogNr1', 'BlogNr2', 'BlogNr3');
?>
Somebody on skype helped me. thanks anyways guys!

Hide and show divs using PHP

So I have this code that requires to be a member. Now the question is what code do I add to hide two different divs called "socialsignup" and "formwrap". Another question is can you have multiple <?php code here ?>s in different places on the same page? Much appreciated with any help.
<?PHP
require_once("./include/membersite_config.php");
if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
?>
You can wrap multiple time of PHP tags in a page.
That's answer for your fisrt and second question.
You can check like this
<?php if($val=='socialsignup') {?>
html for the social sign up
<?php } else if ($val=='formwrap') {?>
html for formwrap
<?php }else{?>
remaining conditions
<?php } ?>

Stacking PHP code?

I've done a project that now needs to be changed in order to display one div if a variable is in an array and a different div if it isn't in the array.
Normally I'd just do
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "quarter code in here";}
else
{echo "nonquarter code in here";}
?>
and be on my merry way, however the code I've got already contains a load of html and php code already, which doesn't like encapsulated within another PHP block (as far as I'm aware?)
e.g.
<?php $quartermonths = array("February","May","August","November");
if (in_array($month,$quartermonths))
{echo "Quarter HTML CODE
<?php quarter phpcode ?>";}
else
{echo "Non-Quarter HTML CODE
<?php non-quarter phpcode ?>";}
?>
So my question is, what is the best way to tackle this? Is it simply to do a javascript hide div A when the variable is met and hide divB when the variable isn't met, or is there a better solution?
Thanks
<?php
if (in_array($month,$quartermonths))
{ ?>
Quarter HTML CODE
<?php quarter phpcode ?>
<?php } ?>
split your html code from php code like this.
It sounds like you just want to concatenate the value of quarter phpcode. Let's say it's a single function, quarter_phpcode(). You can just do this:
{ echo "Quarter HTML CODE" . quarter_phpcode(); }

php if mobile_device_detect is true conditional statement

I don't understand PHP very well... not good start I know.
I'm using http://detectmobilebrowsers.mobi/ for mobile/desktop website, and I'm using wordpress as my CMS.
I have set up my function like so...
require_once('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,true,true,false,false,false);
But now in my theme, I want a conditional statement similar to this...
<?php if (is_home()) { ?>
//Some bits here
<?php } else { ?>
//Some bits here
<?php } ?>
Though I want it to return the values from my function. See below my rubbish attempt below...
<?php if (mobile_device_detect==true) { ?>
//Some bits here
<?php } else { ?>
//Some bits here
<?php } ?>
Obviously this is not going to work lol, but can you see what I'm trying to achieve? and help would be most awesome thanks!
Cheers
Josh
Nearly there.
require_once('php/mobile_device_detect.php');
$mobile = mobile_device_detect(true,true,true,true,true,true,false,false,false);
Then check for
if ($mobile==true) // then do the rest of your stuff

Categories