search.php
session_start();
if(isset($_GET['sessionStart'])){
echo "<br>you start a session";
$_SESSION['session']='yes';
} else {
$_SESSION['session']='no';
}
result.php
session_start();
$sen=$_SESSION['session'];
echo $sen1;
if($sen='no'){
echo $sen2;
echo '<br>Back to search page';
} else {
echo $sen3;
echo '<br>Back to search page';
}
As you can c, if !isset($_GET['sesssionStart']) sen1 and sen3 will be echo,
bu sen3 will echo 'yes'
can anyone help me solvethis ?
Thank you
This is link for search.php
start a session
You use if($sen='no'){this means you set $sen as no. Use == instead of a single = for a comparisson
So:
if($sen=='no'){
This is because instead of comparison you are using assignment in the line
if($sen='no'){ // assigning value 'no' to $sen
Change this line to
if($sen=='no'){ // comparison for equality
if($sen='no'){ to if($sen=='no'){
result.php
session_start();
//also check is required for good code writing
if(isset($_SESSION))
{
$sen=$_SESSION['session'];
}
echo $sen1;
if($sen == 'no'){ // use == in plave of =
echo $sen2;
echo '<br>Back to search page';
}else
{
echo $sen3;
echo '<br>Back to search page';
}
You're using an assignment operator (=) instead of a comparison operator (==) in your if condition. Modify that line to
if($sen=='no')
From the comment of answer owner:
You have to validate if the session has been set or not
if(isset($_SESSION['session']) && $sen == 'no'){
Related
I want to compare if a get variable is a specific text. But I cannot get into the if-statemeant. The echo "invalid user" is not written.
code:
<?php
if ($_GET['status'] && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
when I check the variable status before (ando also after) the if-statemeant with:
echo $_GET['status'];
or with
print_r($_GET['status']);
the result is in both
login_fail
So the variable status is there and has "login_fail".
But "invalid user" is not written!
I tried it also with === or with ' instead " and much much more.
Use isset().
`
<?php
if (isset($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid useenter code herer";
}
`
Try this :
<?php
if (!empty($_GET['status']) && $_GET['status'] == "login_fail") {
echo "invalid user";
}
?>
I think your code don't have any problem.
if your facing a problem till now you can add this line
before your if condition script.
$_GET['status'] = trim($_GET['status']);
In order to avoid errors when $_GET has nothing, use this:
if (!empty($_GET['status']) && ($_GET['status'] == "login_fail")) {
echo "invalid user";
}
Change to test if 'status' is set in the $_GET variable first and then check its value.
if (isset($_GET['status']) && $_GET['status'] == "login_fail")
{
echo "invalid user";
}
That way you will not run into any problems if it's not set.
You should use isset() function to check whether a variable has any value:
if(isset($_GET['status']) && $_GET['status'] == 'login_fail')
{
echo "invalid user";
}
I want to have if the variable = null then he makes it to 1.
if the variable exist do nothing and dont make it again to 1.
i got this code:
if (isset($_POST["register"])) {
if(!$l_NextPage){
$l_NextPage = 1;
echo "helaas" . "</br>";
}
if($l_NextPage == 1){
echo "hoi";
$l_NextPage = 2;
}else if($l_NextPage == 2){
echo "doei";
}
}
only the code dont work i tried empty, isset, $var == FALSE but everytime he makes $l_NextPage to 1. is there any solution i tried this too with session but even it don't work!
What am I doing wrong?
what happen when you refresh page, it assign $l_NextPage = 1 every time, thats why all the time hoi printed
you can use sessions for preserving value of variable after page refresh
try this code
// write this line of code at top of php block
session_start();
if (isset($_POST["register"]))
{
if (!isset($_SESSION["l_NextPage"]))
{
$_SESSION["l_NextPage"] = 1;
echo "helaas" . "</br>";
}
if($_SESSION["l_NextPage"] == 1)
{
echo "hoi";
$_SESSION["l_NextPage"] = 2;
}
else if($_SESSION["l_NextPage"] == 2)
{
echo "doei";
//unset( $_SESSION['l_NextPage'] ); unset varibale
}
}
after reaching at prefixed condition you can unset varible using
unset( $_SESSION['l_NextPage'] );
i have not tested code but this should work
you should try:
if(!isset($l_NextPage)) {
$l_NextPage = 1;
echo "helaas" . "</br>";
}
elseif($l_NextPage == 1) {
(...)
try like this,
if(!empty(trim($l_NextPage)))
I'm trying to compare a POST variable with a string. Can someone help me see what in my PHP code is not written correctly? I've tried both '==' and '==='. Thank you for your help.
$action = mysqli_real_escape_string($mysqli, $_POST['action']);
if(strcmp($action, "save") == 0){
//do stuff
}elseif(strcmp($action, "load") == 0){
//do other stuff
}else{
//do even more stuff
}
why not simply use
if ($_POST['action']=='save'){
}elseif($_POST['action']=='load'){
}
don't understand the mysql in this contenxt
Don't know why you want to do this, but try casting $aciton, like (string)$action.
== is used to see if the two sides of comparison are equal, while === is used to check to see if they're identical meaning they are equal AND of the same type.
As for your code, you should just be able to do
if($action == 'save'){
echo 'save';
}
elseif ($action == 'load'){
echo 'load';
}
else{
echo 'none';
}
I have submitted some code to the redirected url and now trying to use this to echo some information out but can't figure out where I am going wrong.
I have the following code:
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt) == '1'{
return 'failed';
}
?>
all I want to do is if the url has $login_attempt=1 I want to return the message 'failed' to the page.
There is no point of escaping anything if it doesn't enter anywhere important (like a database).
<?php
if ($_GET['login_attempt'] == '1') {
echo 'failed';
}
?>
Also, you have a problem in your if statement, that's corrected in the code above. Be sure to include all of the condition inside of parenthesis, and not just one side of the equality check.
Also, if you wish to display something on the screen, you should echo it, not return.
how about:
if ($login_attempt == '1'){
echo 'failed';
}
Try this one. Your error in $login_attempt == '1':
<?php $login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1'){
echo 'failed';
return false;
}
?>
As others already mentioned you have several problems but the syntax error comes from this:
if ($login_attempt) == '1'{
it should be
if ($login_attempt == '1') {
Dont u think if ($login_attempt) == '1' should be something like this ($login_attempt == '1') Sorry...many others also suggested this :P
At the first, I must tell you that you have a mistake in your IF condition. You typed == outside of ().
In addition, you have to be aware of status of setting your variable through your URL. Check the code below. In this code, I made a function to check the status. Default status is true, and we will check it just for a negative condition. I hope it could be useful for you:
<?php
function check() {
if (isset($_GET['login_attempt'])) {
$login_attempt = mysql_real_escape_string($_GET['login_attempt']);
if ($login_attempt == '1') {
return false;
} else {
return true;
}
} else {
return true;
}
}
if (!check()) echo('Error Message');
?>
When I write an if statement, I have it check a variable like so:
if(isset($_GET['username']){
echo "set";
} else {
echo "unset";
}
How could I get my if statement to check if two variables are set similiar to this:
if(isset($_GET['username'] & $_GET['firstname'])){
echo "set";
} else {
echo "unset";
}
So basically how do I check two things in an if statement at once?
Check out the PHP Manual on control structures and logical operators:
if(isset($_GET['username']) && isset($_GET['firstname'])) {
echo "set";
} else {
echo "unset";
}
Using the single & is doing a bitwise operation, which is most certainly not what you want.
and is also a synonym to the && syntax, as the other answers have shown.
EDIT: As pointed out in the comments, you can check if two variables are isset by passing them both to the isset function. However, if you ever wanted to do some other sort of operation you would need to do the logical operators above.
if ( isset($_GET['username'], $_GET['firstname']) ) {
echo 'Set!';
}
isset takes multiple arguments and if multiple parameters are supplied then isset() will return TRUE only if all of the parameters are set. Evaluation goes from left to right and stops as soon as an unset variable is encountered.
if ( isset($_GET['username']) && isset($_GET['firstname']) )
Try using the && operator (logical and) rather than & (binary and)
so if isset returns true for both then the if returns true otherwise the if will return false.
yes, or
echo ( (isset($_GET['username']) && isset($_GET['firstname'])) ? "set" : "unset" );
if (isset($_GET['username']) AND isset($_GET['firstname']))
{
echo "set";
}
else
{
echo "unset";
}
if (isset($_GET['username']) && isset($_GET['firstname']))
{
echo "set";
}
else
{
echo "unset";
}
For reference: PHP's operators