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.
Related
I'm trying to get a session variable to alternate between 0 and 1 on each page load.
So first time page loads
$_SESSION['turn'] = 0;
Second time
$_SESSION['turn'] = 1;
Third time
`$_SESSION['turn'] = 0;`
and so on.
Then I can call that variable later in the page.
I can't work out how to do this. I've tried a simple IF function but can't get it to work.
First the session must be started on any page wishing to make use of the session array. session_start()
Next you have to remember that initially the session variable you are using will not exist the first time you attempt to use it
So
<?php
session_start();
if ( !isset($_SESSION['turn']) ) {
// does not exist yet, so create with 0
// you may want to initialize it to 1, thats up to you
$_SESSION['turn'] = 0;
} else {
$_SESSION['turn'] = $_SESSION['turn'] == 0 ? 1 : 0;
}
Try this where the page is loaded.
$_SESSION['turn']=1-$_SESSION['turn'];
code:
<?php
session_start();
echo $_SESSION['turn'];
$_SESSION['turn']=1-$_SESSION['turn'];
?>
Edit : RiggsFolly !isset() is correct. mine misses it and it will give errors in log. and the first value is not 0
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.
In this code I'm trying to ban client if he/she/it doing to much(10) login request for 3 minutes. The problem is after 3 minutes user must refresh the page 2 times. I can see the reason why it's enter into if statement but I can't find the solution. I feel like I've overcoded.
if($this->sessions->get_data("wrong_login")>10){
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time());
}else
{
if(time() - $this->sessions->get_data("ban_time") > 180){ // 180 seconds
$this->sessions->remove("ban_time");
$this->sessions->remove("wrong_login");
}
}
// The message if user still banned
die("Banned for 3 minutes!");
}
I hope I can tell the problem..
EDIT: This code is the inside of the construct of register controller.
Before your IF statement, add another if statement that checks for ban_time session if the time is up, then set the wrong_login session to 0 if it is.
if($this->sessions->get_data("ban_time") < time())
{
$this->sessions->remove("ban_time");
$this->sessions->set_data("wrong_login", 0);
}
remove your else statement there.
also forgot to mention! when you set the ban time, it should be time() + 180.
if(!isset($_SESSION["ban_time"])){
$this->sessions->set_data("ban_time", time()+180);
}
use header function.
e.g.
header("Location: /path/to/some/file.php?error=Banned for 3 minutes.");
Then on the file.php you can do this:
<?php
// Parse error
$error = isset($_GET['error']) ? $_GET['error'] : '';
// Display error (if any) and stop executing the rest of the code.
if (!empty($error)) {
exit($error);
}
?>
This will not work if you already started to output...
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.
I think this code should echo "first" in first usage and after refresh it should echo timestamp,but it will show first every time,where is my problem?
I set cookie permition on always.
if (isset($_SESSION['TestSession']))
{
echo ($_SESSION['TestSession']);
}
else
{
echo "first";
$_SESSION['TestSession'] = time();
}
Have you placed
session_start();
at the top of your page?
To start a session, first session_start() should be called. Otherwise $_SESSION won't be set, and you will initialize it every time.