Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I would like to set a javascript variable 'logged_in' to either 1 or 0, depending on whether a user is logged in Wordpress. This is what I have:
<script type="text/javascript">
var logged_in = <?php if ( is_user_logged_in() ) { echo '1';} else {echo '0';} ?>;
</script>
But it's not working. What's wrong with it?
I just checked your coding. Your code will define the variable you declared in your script as 1 if true and 0 if false. Keep in mind that you are only defining a variable and it will not do anything as it is. Here is what I have. Just take a look your source code.
<?php
function is_user_logged_in() {
return true;
}
?>
<script type="text/javascript">
var logged_in = <?php if ( is_user_logged_in() ) { echo '1';} else {echo '0';} ?>;
</script>
If it is not setting the variable make sure that the function is_user_logged_in() exist and is available on the script.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I would like to have 2 things happen if an if statement condition is met. If isset is true the variable $value will equal 1. The problem is that I also want the value of a session variable $_SESSION['counter'] to increment by 1, but I am unsure of the syntax.
<?php
session_start();
$_SESSION ['counter']=0;
function checkbox_boolean ($name){
if (isset($_POST[$name])) $slot1 = '1'; $_SESSION['counter']++; else $slot1='0';
}
This will work and its a better solution:
<?php
session_start();
$_SESSION['counter']=0;
function checkbox_boolean ($name){
$slot1=0;
if(isset($_POST[$name])){
$slot1 = 1;
$_SESSION['counter']++;
}
}
?>
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I declared an array
$CLISTS=array("Add_Product"=>"products.php","Payment_Type"=>"payment.php","Shipping"=>"shipping.php");
and I defined variables
<?php
define("Add_Product",TRUE);
define("Payment_Type",FALSE);
define("Shipping",FALSE);
foreach($CLISTS as $lists=>$page)
{
if($lists==TRUE)
{
?>
<div class='alert' style="text-decoration:line-through;"><?php echo str_replace("_"," ",$lists);?></div>
<?php }
else
{
?>
<div class='alert'><?php echo str_replace("_"," ",$lists);?></div>
<?php }
}
?>
Its not working. All the div is strikes. What I did mistake?
DEFINE does not do what you think it does. Define creates a named constant.
And you cannot change your array variables with it.
Simply do:
$CLISTS['Add_Product'] = true;
$CLISTS['Payment_Type'] = false;
$CLISTS['Shipping'] = false;
To change your array variables.
You can write the logic like
foreach($CLISTS as $lists=>$page)
{
if($lists == 'Add_Product')
{
?>
Or even you can use === like
foreach($CLISTS as $lists=>$page)
{
if($lists === TRUE)
{
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to try this. But I don't know that this will work or not and whether this is a good way for me to achieve what I want.
What I want is to show different page view for users (login and not login user)
I am going to use this method with session, but please check if I do it correctly:
<?php
session_start();
if ( $_SESSION['login_id'] == 0 || $_SESSION['login_id'] == '' ) {
echo "user is not login, and I will show the not login page view to them";
} else {
echo "user is login, and I will show the login page view to them";
}
require_once('configPDO.php');
?>
If you stored the variable in $_SESSION in the right way, you can redirect the user in different pages using header after the login.
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id'])){
header("location:PAGELOGIN.php");
}
else{
header("location:PAGENOTLOGIN.php");
}
?>
if you want to stay in the same page, you can do:
<?php
session_start();
if(isset($_SESSION['login_id']) && !empty($_SESSION['login_id']))
{
?>
YOUR HTML CODE
<?
} else {
?> YOUR HTML CODE
<?}
?>
this will show your html code using the php condition. tell me if this work ^^
try this...
<?php
session_start();
if(isset($_SESSION['user'])){
echo "iam login";
}
else{
echo "not login";
}
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
session_start();
$user = $_SESSION['username'];
if( isset($_POST['subm_btn']) ) {
incrementClickCount();
}
function getClickCount()
{
return (int)file_get_contents($user.".txt");
}
function incrementClickCount()
{ $count = getClickCount() + 1;
file_put_contents($user.".txt", $count);
}
User register on my site, then he click on button (name="subm_btn"). I want count clicks and add number of clicks in file with name "username.txt"
I guess you are looking for something like this:
$file=$user.'.txt';
incrementClickCount($file);
function incrementClickCount($file){
$count = getClickCount($file) + 1;
file_put_contents($file, $count);
}
function getClickCount($file) {
return (int)file_get_contents($file);
}
If you want the variable to be available inside a function you either make it global or pass it as an argument (which is better).
You define $user, and then access $user1. That would be my guess as to why it doesn't work. Also, using $file might be a better idea anyway.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm finding an organisation a major problem with mixing PHP and HTML, it just looks horrible, so i'm wondering if it's a viable option to create a set of object oriented methods such as this:
class MainOO {
public $Database;
public function __construct($Server,$User, $Password, $DB){
if ($this->Database = new mysqli($Server,$User,$Password,$DB)){
return true;
}
return false;
}
public function User_Login(){
$Get_Usr_Info = $this->Database->prepare("SELECT ID, Password, Salt FROM Users WHERE Username=?");
$Get_Usr_Info->bind_param('s',$_POST['username']);
$Get_Usr_Info->execute();
$Get_Usr_Info->store_result();
$User_Number = $Get_Usr_Info->num_rows;
$Get_Usr_Info->bind_result($UserID, $Stored_Password, $Stored_Salt);
$Get_Usr_Info->fetch();
$Get_Usr_Info->close();
if ($User_Number !== 1){
$Error = "Wrong Username Specified Or Password Is Incorrect";
header ("Location: index.php?Errors=".urlencode($Error));
exit;
}
// Continue with login script
}
public function Logout(){
if (session_status() !== PHP_SESSION_DISABLED){
session_destroy();
header ("Location: LoggedOut.php");
exit;
}
}
}
Then HTML side:
<?php
include "MainOO.php";
$MainOO = new MainOO("host","user","password","database");
?>
<div class="example">
<div class="example left">
<?php
$MainOO->User_Login();
?>
</div>
</div>
It's still mixing PHP & HTML, but it's making look a hell of a lot neater than having heaps of PHP in the middle of HTML.
I'm fully aware I could migrate over to a MVC Framework (which this topic is looking like) already setup, or even use a template engine such as smarty, but I want to avoid this as much as possible.. So is this a viable option to have neater PHP code within html?
You will probably want to use an isset() in the code too
e.g
<?= (isset($variable)) ? $variable : ''; ?>
e.g if variable isset then display it otherwise display nothing