Wondering how to execute html in if else statement using php - php

I am very new at PHP and I don't even know if this is possible but I was wondering if it was possible to only show the comment section only if the user is logged in. I use the following the check if the user if logged in or not.
<?php if (isset($_SESSION['name'])){
/* this displays the comment section */
<div class="fb-comments" data-href="https://mywebsite.com" data-width="650" data-numposts="10"></div>
} else {
echo "Please login";
}
?>

do it like this and let me know.
<?php if (isset($_SESSION['name'])){ ?>
<div class="fb-comments" data-href="https://mywebsite.com" data-width="650" data-numposts="10"></div>
<?php } else {
echo "Please login";
}
?>

enclose the div to echo. Everytime you want to print HTML code inside of a PHP code just print it using echo "html code here";
echo '<div class="fb-comments" data-href="https://mywebsite.com" data-width="650" data-numposts="10"></div>';

Related

Trouble returning session data (user name)

I'm trying to integrate a php login script that I have working, but I can't seem to get simple php calls going on a page. On this user profile page, I want to simply have the user name displayed (mysql field is "name"). The user is logged in and the session carries through, but on this page, all I see is the text "Here is your profile info..." What might be wrong in the code to prevent the user name from displaying?
<?php
include_once('classes/check.class.php');
include_once('header.php');
if( protectThis("*") ):
if(!isset($_SESSION)) {
session_start();
}
if(isset($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
?>
<br />
Here are is your profile info...
<?php
else :
?>
<div class="alert alert-warning">
<?php _e('Only signed in users can view what\'s hidden here!'); ?></div>
<?php
endif;
include_once('footer.php');
?>
For check session is set already use session_id() Also check you have set $_SESSION['jigowatt']['name'] already with empty()
if(session_id() == '') {
session_start();
}
if(!empty($_SESSION['jigowatt']['name'])) {
echo "You're name is: " . $_SESSION['jigowatt']['name'];
}
else {
echo 'username is empty';
}
You need to put session_start(); at the very top of the page. No white space can be put before that. Try if that works.
First you need to write the sessions at the very top of the page if it works than okay else you can try this.
Just append this 2 function before and after the session_start();
Like this
ob_start();
session_start();
ob_end_clean();

How do I add If else statement to a snippet

I found this great php code that will help me serve adsense ads only to search traffic.
I need help coding it so that if the user is not coming from search then another ad code is shown to the user.
This is the original code
<?php if (function_exists(‘from_searchengine’)) {
if (from_searchengine()) { ?>
INSERT YOUR ADSENSE/BANNER CODE HERE
<?php } } ?>
What I am looking to do is if the user is not from search show another banner code.
Can someone help me with editing the code?
I don't think it's the best practise what you're doing but still:
<?php if (function_exists(‘from_searchengine’)) {
if (from_searchengine()) { ?>
INSERT YOUR ADSENSE/BANNER CODE HERE
<?php } else { ?>
INSERT ANOTHER AD CODE HERE
<?php } } ?>
but it looks better when you do that:
<?php
$ad_code = "INSERT YOUR ADSENSE/BANNER CODE HERE";
$another_ad_code = "INSERT ANOTHER AD CODE HERE";
if (function_exists(‘from_searchengine’)) {
if (from_searchengine()) {
echo $ad_code;
}else{
// if not from search engine
echo $another_ad_code;
}
} else{
// if function doesn't exist
echo $another_ad_code;
}
?>

stop preventing HTML code rendering with Return and Die function in php

As you know in a php script when you call Return or Die it prevents the rest of HTML codes from rendering.
In an occasion when I just want to stop the php script but not whole the page what would I do?
Ex:
<?php
if(!isset($_POST['txt_username']))
{
echo "Please enter your username";
return;
}
?>
<i want="this html">to be rendered</i>
I want my HTML codes to be rendered afterward.
Thanks for reading.
Your question is not clear, but you need to stop the PHP code to execute, but the HTML to render? You might need output buffer or functions.
E.g.:
<form action="" method="post" >
<input type="text" name="txt_username" />
<input type="submit" />
</form>
<?php
function doSmth(&$password) {
if(!isset($_POST['txt_username']))
{
echo "Please enter your username";
return false;
}
$password .= "333";
echo "You password has been changed to $password";
}
$password = 128;
doSmth($password);
?>
<body>
<p> <b> Your password is <?= $password; ?> </b></p>
</body>
Examples:
Text field is set:
Output:
You password has been changed to 128333
Your password is 128333
Text field is not set:
Output:
Please enter your username
Your password is 128
Use the conditional statement to include more PHP is needed. The HTML can then be rendered regardless of the PHP's if/else statement. Example:
<?php
if( !isset($_POST['txt_username']) )
{
echo "Please enter your username";
}
else
{
//Some dditional PHP functions
}
?>
<i want="this html">to be rendered</i>
<?php
$stateOfRender = false;
if(!(isset($_SESSION['sessionid']))) {
echo "You have not logged yet. Now you are redirecting to Login Page";
header('Refresh: 5; URL = login.php');
} else {
$stateOfRender = true;
}
// In everywhere you can use this variable to control your statement
// for example:
if($stateOfRender) {
// Open DB connection and fetch user data and settle it to page.
}
?>
In my php code i use this method to handle process. Generally i use it in db connection. If dbconnection is success i render page with control this variable. I hope to it helps you.

JQuery & PHP replaceWith

I have a php file containing my website's login script.
If the user doesn't enter the correct password, then the else is executed (see code below):
PHP
else {
echo "<script type='text/javascript'>";
echo "$('.error').replaceWith('<h2>Your password is wrong!</h2>')";
echo "</script>";
}
and I have a blank h2 for the error to be replaced onsubmit:
<div id="error_space">
<h2 class="error"></h2>
</div>
The only problem is that the error won't get replaced...
Hope that someone can help!
EDIT: I echoed it directly as suggested below.
Thanks.
You're missing a ' from your line
echo "$('.error').replaceWith('<h2>Your password is wrong!</h2>)";
^ here
u just forgot '
else {
echo "<script type='text/javascript'>";
echo "$('.error').replaceWith('<h2>Your password is wrong!</h2>')";
echo "</script>";
}

Php login hide when logged in and hide logout when not logged in

I want a code to hide login button when logged in and display "welcome,username" and the logout button and when not logged in to display the login button...
http://needforgaming.x10.mx/testefinal/home.html This is my website you can see how it looks idk if it is where i put the code thaths wrong or the code
<?php
session_start();
if(isset($_SESSION['username'])){ ?>
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>
<?php } else{ ?>
<p>Login</p>
<?php } ?>
There is another easy method. You can start session firstly. Then use this code:
<?php
if(!isset($_SESSION['user']))
{
echo '<li>Login</li>';
}
else
{
echo '<li>Logout</li>';
}
?>
actually this code is for using login/logout in . You can use it where ever you need. Just change the html tags. That's it
I think you are using plain html .. your page needs to be of php to run this code on a apache/IIS server, code is good enough to work.
You may want to check for emptiness. Change:
if(isset($_SESSION['username'])){ ?>
to
if(isset($_SESSION['username']) && !empty($_SESSION['username'])){ ?>
You've put PHP code to .html page. Either switch it to .php, or configure your server to evaluate .html as PHP too.
Take a look at Your first PHP-enabled page, it should provide sufficient details.
you should make all the block a php code:
<?php
session_start();
if(isset($_SESSION['username'])){
echo "<p>Ola; <u> $_SESSION['username']; </u>, </p>";
echo "<p>Logout</p>";
} else{
echo" <p>Login</p>";
}
?>

Categories