Get value in URL PHP - php

When I submit a form with wrong id's, the url is set like this :
url-test.com/connexion/?login-failed
I want to trigger login-failed to make conditions.
Here's my code, but it does not work
$login_failed = $_GET['login-failed'];
if($login_failed) {
$error = 'Oups failed';
}
//My form
<?php wp_login_form(); ?>
<p><?php if(isset($error)) {echo $error;} ?></p>
Thanks for your help !

if(isset($login_failed)) {
$error = 'Oups failed';
}
You have to check that it's isset.

Your code isn't working, because you're store the value of $_GET['login-failed], but actually there's no value. If you change ?login-failed to ?login-failed=1, it should works.
Otherwise, you should check if variable isset, e.g:
$login_failed = isset( $_GET['login-failed'] ) ? true : false;

Related

if statement on session data codeigniter

So I am trying to hide or show options if the user is logged in or not.
I have this simple statement, its always showing no regardless if I am logged in or not.
if( !isset($_SESSION) ){
echo "yes";
}
else {
echo "no";
}
I have also tried
function __construct()
{
parent::__construct();
$this->is_logged_in();
}
if(is_logged_in())
{
echo "yes";
}
else
{
echo "no";
}
Neither works, I also think the first one if simpler, but I am not sure what method would be better.
isset($_SESSION) checks if the variable is set or not and here $_SESSION is already defined.
so in your case !(isset($_SESSION)) is false coz isset($_SESSION) is true and !true is false
To check for the session value try isset($_SESSION['key_you_set']). This will check if the key_you_set exists or not.
Assuming you have set an session with name session_id
You can retrieve your session information in codeigniter like,
$session_id = $this->session->userdata('session_id');
Now You can check like below
if($session_id==""){
echo "session not set";
}
else{
echo "session set";
}
I think this helps you ..
$session_id = $this->session->userdata('session_id');
less code but more secure
echo (!empty($session_id ) && isset($session_id) ) ? "session set" : "session not set" ; // ternary operator

isset() function always returns true [closed]

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've been figuring out how to do this but it seems not to be working.
My code for the form:
<form name="register" method="POST" action="php\reg.php">
<label>Email:</label>
<input type="email" name="email"/>
.
.
.
</form>
Code for the "reg.php" file:
if (isset($_POST['email'])) {
echo "success!";
} else {
echo "empty!";
}
?>
Problem is, whenever I input or not in the email textbox, isset function always returns the true value. how can I resolve this??
All answers that tell you about using isset() and empty() to check form values are... bad. Use one of the filter functions to check if a POST/GET/COOKIE variable is present and validate or sanitize it. Here is an example:
$email = filter_input(INPUT_POST, "email", FILTER_VALIDATE_EMAIL);
if ($email === NULL) {
die("Email field not present in form");
} elseif ($email === FALSE) {
die("Email is present but its value is invalid");
}
Note: the empty() function has a gotcha and I would not recommend using it for form validation unless you understand exactly what it does: it returns true for all falsy values. This means 0 is considered empty which is often an acceptable value in certain cases. Example (bad one):
if (empty($_POST["number_of_children"])) {
// triggers even when user enters 0
die("number of children field is empty");
}
isset() checks if the values is set, so it's true even if the value is empty string. Use empty():
if (empty($_POST['email']) == false) {
echo "success!";
}else{
echo "empty!";
}
<?php
if (!empty($_POST['email'])) {
echo "success!";
}else{
echo "empty!";
}
?>
try this-
<?php
if (!empty($_POST['email'])) {
echo "success!";
}else{
echo "empty!";
}
?>
Null and/or empty strings are still set if the variable is declared.
$_POST contains empty string for your value, and isset() returns true. Use empty() or add != '' in your if condition.try this:
if (!empty($_POST['email']))
Try to use empty() function to check if there is anything in email filed of $_POST array.
The example code based on your code is below:
<?php
if (isset($_POST['email']) && !empty($_POST['email'])) {
echo "success!";
} else {
echo "empty!";
}
?>
<?php
if (isset($_POST['email']) && $_POST['email'] != "") {
echo "success!";
}else{
echo "empty!";
}
?>
try this way

Numeric PHP validation?

I have some code written in php to validate a postcode field in my form. The code is meant to check that the field is not empty (mandatory) and matches one of 5 usable postcodes, if not it displays an alert. The problem i am having is that when i leave the field empty and hit the submit button the proper alert is show but if i enter a wrong value and hit submit the form just loads to a blank screen, can anyone spot a mistake in my code? :
<?php
if (isset($_POST['submit'])) {
$post = $_POST["post"];
$words = array('2747','2750','2753','2760','2777');
$msgp = "";
if (!empty($post)) {
foreach ($words as $item)
{
if (strpos($post, $item) !== false)
return true;
}
$msgp = '<span class="error"><b>Please enter correct postcode</b></span>';
return false;
} else if(empty($post)) {
$msgp = '<span class="error"><b>Please enter postcode</b></span>';
}
}
?>
<form name="eoiform" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi">
<b>Post Code</b>
<br>
<input type="text" id="post" name="post" /><?php echo $msgp; ?>
</form>
return? Return where?
When you return in your main code, it's (nearly) the same as die()'ing.
So when you return, the remaining PHP won't be executed anymore.
I'd consider to set some variable like $success = true/false; instead of returning.
You return false after $msgp = '<span class="error"><b>Please enter correct postcode</b></span>'; therefor you do not continue to the form below... remove the returns from your code to be able to handle and show an error.
You are using return. Are you in a function() {} ? If so, all your variables are in function scope. You could do global $msgp; to make the variable accessible outside of the function.
If not... then you shouldn't use return.
Change php code to
<?php
if (isset($_POST['post'])) {
$post = intval($_POST["post"],0);
$words = array(2747,2750,2756,2760,2777);
$msgp = '<span class="error"><b>'.in_array($post,$words) ? "Please enter postcode" : "Please enter correct postcode" .'</b></span>';
}
There are a lot of things that can be simplified. For example codes as int array and using in_array() function. Return statement should be used to return something from method/function using it in global scope then execution of the current script file is ended. There is no need for it here. If there are more $words values you should consider using simple REGEX /[0-9]{4}/ and preg_match()
You want to display $msgp right? Use echo $msgp;. But you can return to nowhere in your code. (put it in a function).
Try this
<?php
$post = $_POST["post"];
$words = array('2747','2750','2753','2760','2777');
$msgp = "<span class='error'><b>Please enter correct postcode</b></span>";
function check($post){
global $words;
$return=true;
if (!empty($post)) {
foreach ($words as $item)
{
if (strpos($post, $item) !== false)
//$return=true;
}
} else if(empty($post)) {
$return=false;
}
return $result;
}
$return=check($post);
if($return === true){
// echo you are right!
}
else {
echo $msgp;
}

How to check if an input passed in $_POST is not empty? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check whether $_POST-value is empty
I am using this code to validate if the passed input is not empty, but this fails when I use "white space" in the input. It does pass the empty() check.
if (empty($_POST['r_user'])) {
$data['error'] = 'Please pick a username to continue.';
$data['errorID'] = 'r_user';
}
What is the proper method to check an empty string in PHP?
You need to use trim function before checking the variable.
$trimUser= trim ($_POST['r_user']);
now execute the empty method.
as per PHP documentation suggestion, your code would become:
if (trim($_POST['r_user']) == false) {
$data['error'] = 'Please pick a username to continue.';
$data['errorID'] = 'r_user';
}
you can trim() removes white space from the beginning and end of a string
working example
$test = " ";
if(trim($test)){
if (empty($test))
echo "true";
else
echo "false";
}
live : check this on codepad
You may use :
$var = '';
if (array_key_exists('r_user', $_POST)) {
$var = trim($_POST['r_user']);
}
if (empty($var)) {
// do some stuffs
}
instead.
You can use trim nicely here, it removes whitespace front and back:
if (empty(trim($_POST['r_user']))) { // Incorrect!
Edit, wow, learned a new thing today.
This will however work:
$var=trim($_POST['r_user']);
if (empty($var)) {
Working Example:
<?php
$var=' ';
$var=trim($var);
if(empty($var))
echo "Yay";
else
echo "Nay!";
?>
Output:
Yay
if ( empty( trim( $_POST['r_user'] ))) {
// wrong! gives an error - learned something new :)
// the right way should be :
$myvar = trim( $_POST['r_user'] );
if ( empty( $myvar )) {
or
if ( trim( $_POST['r_user'] ) != '' ) {
if(isset($_POST['r_user'])){
// ur code
}

php and $_GET array

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');
?>

Categories