Only one line executed in a false IF - PHP - php

I'm integrating pagination to a wordpress block, what should happen is whenever i click "older posts" button, the post offset is incremented in another php file (the block file and the increment files are separate files).
Whenever I click the older posts however some magic happens that shouldn't:
The problem is with one of the if statements, what happens is that IF statement evaluates to false, yet that one line that I marked in the code still gets executed (the echo doesn't execute but the $_SESSION["inf"] = 0; does).
Here is the code.
if(isset($_POST["paginate_btn"])){
if(isset($_SESSION["inf"])){
echo "PREVIOUS:".(string)($_SESSION["inf"]);
$_SESSION["inf"] = $_SESSION["inf"] + 5;
echo "<br/> incremented by 5, RESULT:".(string)($_SESSION["inf"]);
}
else{
echo "<br/> isset session inf failed, set to 5";
$_SESSION["inf"] = 5;
}
}
else if(isset($a) and ($a != $url)){
echo "<br/>dif page";
if(isset($_SESSION["inf"])){
echo "<br/> unset";
$_SESSION["inf"] = 0; <----- THIS LINE HERE!
}
else{
echo "<br/>session inf not set and button not pressed";
}
}
else{
echo "<br/> button not pressed, a is not set and is same page";
$_SESSION["inf"] = 0;
}
I've messed around with this for hours and hours, I don't understand what the case is here..
var_dump((isset($a) and ($a != $url)));evaluates false, yet still only that one line of code there gets executed, the echo doesn't, not even the dif page echo before it....
And the funny part, if I comment out that one line, this no longer happens, I've even tried setting it to different variables there so it definitely gets run solo somehow.
Var_dump code after the if statements.
echo "<br/>--1---<br/>";
var_dump((isset($a) and ($a != $url)));
echo "<br/>--2---<br/>";
var_dump((isset($_SESSION["inf"])));
echo "<br/>---<br/>";
echo $_SESSION["inf"];
echo "<br/>---<br/><br/>";

Another part of the code sets $_SESSION["inf"] to 0.
If you don't see the echos, they don't get executed.

Related

XAMPP php executing both if and else parts

It is strange. My php code is executing both if and else parts. Following code starts where $_SESSION['abc'] is not set yet.
//echo $_SESSION['abc'];
if(!isset($_SESSION['abc'])){
echo "not showing this echo";
$_SESSION['abc'] = "new";
} else {
echo " why it jumps here, in XAMPP?";
}
My actual code was more complex so I did it simpler as above. The above code is not echoing "not showing this echo"; even when $_SESSION['abc'] is not set. If I comment out the statement $_SESSION['abc'] = "new"; it echoes "not showing this echo".
What I found out by now:
It is happening only in XAMPP, at production server same is working fine.
It happens when I make the else condition true in the if block. And that condition is setting a session variable.
I myself thought the code is executing twice or something but it is not.
And more importantly "if" condition is true, if you uncomment first line echo $_SESSION['abc'];, it will show undefined index error.
The weirdest feeling for me is, when it reaches $_SESSION['abc'] = "new"; in "if" block, it jumps to "else", ignoring the echo line before it.
Weird!! Am I missing something or it is a XAMPP bug. Yes, the code is working as expected, on production server.
EDIT:
I would like to add that only for this behavior I also reinstalled XAMPP (for the latest version).
And as to "how do I know if it sets session variable, while not echo the line before it", it prints echo $_SESSION['abc']; in else-block, but it shows undefined index error if I uncomment first line echo $_SESSION['abc'];
Code with echo line added in else-block.
//echo $_SESSION['abc'];
if(!isset($_SESSION['abc'])){
echo "not showing this echo";
$_SESSION['abc'] = "new";
} else {
echo " why it jumps here, in XAMPP?";
echo $_SESSION['abc'];
}
UPDATE 2018-06-03
I was doing the above code in Laravel. I tried the above code out of Laravel framework in a plain PHP file and it worked in XAMPP, as it should. So the new information is, this is not working in Laravel-XAMPP combination. Versions: Laravel 5.4 and XAMPP 7.2.5
NOW??
I'm not sure what you want to achieve. It looks like you are testing $_SESSION before you assign a value to your session key.
Do a print_r ($_SESSION) to become clear when which value is assigned. And which value is stored after you reload your session. You can also printout your statement to see the result like echo (!isset(value)) as an example.
session_start ();
print_r ($_SESSION);
if (!isset ($_SESSION ['whatever'])){
echo "not set";
print_r ($_SESSION);
}
else {
echo "set";
print_r ($_SESSION);
}
Also try to change the first statement to be true if set and assign your new value in the else block
session_start ();
print_r ($_SESSION);
if (isset ($_SESSION ['whatever'])){
echo "set";
print_r ($_SESSION);
}
else {
echo "not set";
print_r ($_SESSION);
$_SESSION['whatever'] = "new value";
}
Hopefully it helps to find the faulty place
Edit
Be sure to use
session_start()
at very first line
or to set
session.auto_start = 1
in your php.ini

Store time in a session

I have a code that works just fine but I have a few questions about it. I don't understand the logic of something. The code is:
<?php
session_start();
if(!isset($_SESSION['t0']))
{
$_SESSION['t0']=time();
echo $_SESSION['t0']."if<br />"; //why this is never printed?
}
else
{
if(time()>=($_SESSION['t0']+3))
{
echo $_SESSION['t0']."else-ul";
$culoare="rgb(".rand(0,255).",".rand(0,255).",".rand(0,255).")";
$_SESSION['t0']=time();
}
}
?>
The questions would be:
1. Why the first echo is never printed?
2. Why (time()>=($_SESSION['t0']+3)) isn't always true since $_SESSION['t0'] is updated every second because of session[t0]=time() ?
Thank you!
First echo statement does get executed, but it happens only on very first time. Once you had your session started value for $_SESSION['t0'] is always set, so the if condition will always return false.
time()>=($_SESSION['t0']+3) condition is true when 3 seconds has passed after execution of the code. So if you reload your page after 2 seconds it will not get executed.

After i delete the object, the page has to be refreshed manually for the update to take place on the screen?

Here is my code:
for($i = 0; $i < $printcoll->getlineCount(); $i++){
$li = $printcoll->getLineItem($i);
$item = $li->getItem();
if($item instanceof Product){
print "Bike ID - ";
}
print $item->getId();
if($item instanceof Product){
print "&nbsp Price &pound";
}
print $item->getPrice();
if($item instanceof Product){
print "&nbsp Quantity - ";
}
print $li->getQuantity();
print "<a href='myOO.php?delete=" . $i . "'>Delete</a>";
echo "</br>";
}
if(isset($_GET['delete'])){
$id = $_GET['delete'];
$li = $printcoll->getLineItem($id);
$printcoll->delLineItem($li);
}
This code deletes the object the user specifies (by clicking the a href). However, after clicking delete, the page does not go back to "myOO.php". It stays as "myOO.php?delete=".$i." and so i have to manually delete the "?delete=".$i." bit out of the address bar for the update to appear on the page. Any ideas?
One solution is putting ob_start(); at the start of the page, and then putting header('location:myOO.php') in the if statement. However im not sure this is the correct way of doing this. Any ideas? Thanks
Ask kennypu already pointed out, PHP code is executed on the webserver.
This means that everytime you change the document on the server, the page must be reloaded on the client manually or automatically ( e.g. by using AJAX ) in order to re-execute the PHP code on the server.

PHP counter file and page forwarding

So i'm writing this code so that you either get forwarded to a certain page if you're the first one to hit the link, or you are sent back to the original page after being displayed a message if you're not what beginner mistake am i making?
<?php
$count = file_get_contents('counter.txt');
$count = trim($count);
if ($count="0")
{
$count = $count + 1;
$fl = fopen("counter.txt","w+");
fwrite($fl,$count);
fclose($fl);
header("Location: newpage.html");
}
else
{
fclose($fl);
echo "Sorry but the item has already been sold out";
header("Location: oldpage.html");
}
?>
As for the delay, you can accomplish it two different ways. The first is to use PHP header (like you are currently doing), but change it to look like this:
<?php
header("refresh:5;url=oldpage.html");
echo "Sorry but the item has already been sold out";
?>
The other way is to echo out a piece of HTML code, the meta-refresh:
<?php
echo '<meta http-equiv="refresh" content="2;url=oldpage.html">';
echo "Sorry but the item has already been sold out";
?>
In both examples, 5 is the amount of seconds until the refresh. Experiment with each one to see if it will fit your needs.
This might be some sort of syntax that I'm not familiar with, but none of my scripts have ever had the
<? code
I simply use
<?
Also since you did not delay our header tag the user will not see the previously echoed statement above it. It will automatically redirect before the page has time to output fully.

While statement repeats infinitely

I have a website where i need to use a while statement, but when i use it, it repeats the echo infinitely. Although it looks like i could make it work without while, that isnt so, this is a simplified version of a final product that will need while.
<?php
$passlevel = '0';
while ($passlevel == '0')
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel == '1';
}
else
{
echo "you didn't select purple.";
}
}
else echo "contact webmaster";
}
?>
Why is it echoing either contact webmaster or you didnt select purple an infinite number of times?
First, you probably need to change:
$passlevel == '1';
to
$passlevel = '1';
The first is a comparison equals, not an assignment equals.
Second, if $color is not #800080, then the loop does not terminate and thus repeats forever as nothing in the loop causes the value to change.
I'm not entirely sure of the point of this loop in the first place. It should work perfectly fine without the loop, however you've stated that your code is a simplified version of something more complicated that indeed needs a loop. Perhaps you can elaborate.
You're not providing any way out of the loop. If $_GET['box_1_color'] isn't purple the first time through the loop, it can't possibly become anything else the second time through the loop, so it'll keep being the wrong color each and every time.
I'm not certain what you intended for this loop to accomplish. If you're trying to have the user enter a new value each time, you won't be able to do that with a loop in PHP. You'll have to regenerate the entire page (with an error message, presumably) and ask the visitor to submit the form again.
In the case of "contact webmaster", you need to break out of the loop, either with the break expression or by setting your $passlevel to anything other than zero. A more serious real problem is revealed in #Mike Christensen's answer, though
If $_GET['box_1_color'] is not set, the variable $passlevel will never be changed.
<?php
$passlevel = 0;
while ($passlevel == 0 || $passlevel == 2)
{
if(isset($_GET['box_1_color']))
{
$color=$_GET['box_1_color'];
if($color == "#800080")
{
echo "you have passed step one.";
$passlevel = 1;
}
else
{
echo "you didn't select purple.".'try again.';
}
}
else
{
echo "contact webmaster";
$passlevel = 2;
}
}
?>
You need to define another passlevel for failure, to stop the while loop. Also, don't put any quotes around integers.

Categories