GET POST errors - php

All, I posted a code in a forum before and no one was able to answer this.
The "Sign Up" and "logout" are both buttons with value types on other pages that are linked to this page code called login.php
The problem is that I keep getting an undefined index. Is there a way to call it better?
I have..
if ($_POST['submit']=="Sign Up") {
and..
if($_GET["logout"]==1 AND $_SESSION['id']) { session_destroy();
header("Location:logout.php");
}

As both POST and GET variables must not be send at all, always use a scheme similar to this one:
$var = isset($_POST['fieldname']) ? $_POST['fieldname'] : null;
if ( !isset($var) )
{
// errorhandling
}
else
{
// proceed ...
The error you get indicates that at least one of your POST and/or GET variables is either not set or misspelled.

As Axel points out, check if $_POST has the value for "submit" using isset() function, if it is set, the you can access the value the way you are doing. If $_POST does not have the value for that, it means that it is not being sent.
So, in your HTML, check that the button is indeed called that way and that it is inside a tag with the ACTION attribute and METHOD set to POST.
Also be careful with comparing with 1, since in PHP is also means true.

Related

if condition changed by its content

I encountered relay interesting problem.
I have this condition in external file which is called and after process code redirects to page set via BACKLINK post parameter like this.
if(isset($_POST['secureLogout']))
{
$_POST['backlink'] = '/';
session_destroy();
System::WriteLog('Session has been destroyed');
}
and even I don't post secureLogout parameter it still trigger, so I tried to var_dump POST variables, there is no secureLogout parameter.
so then I tried put my POST inside condition with exit to see what is in my post..
if(isset($_POST['secureLogout']))
{
var_dump($_POST); exit();
$_POST['backlink'] = '/';
session_destroy();
System::WriteLog('Session has been destroyed');
}
and NOW magic happens It does not trigger the code continue and everything works fine, but i cant logout now directly cause of exit before session destroy.
So basically how is it even possible that content inside IF has any effect on condition??
It is completely UN-logical and I lost my patience, I tried change post names, conditions but result is same.
If there is exit inside condition, condition is false, if there is no exit condition is true.
EDIT
I changed if condition to $_POST['secureLogout'] == 'secureLogout' problem still remained then i changed condition to
$_POST['secureLogout'] == 'secureLogout123'
which has to be false everytime and it is session is not destroyed, so I put exit inside again and changed condition back
$_POST['secureLogout'] == 'secureLogout'
with exit, condition does not tirgger so $_POST secureLogout parameter is either different or is not set
I removed exit and session been destroied...
Check the amount of parameters available to be post in phpini. Maybe you are trying to post too many variables which overflow the capacity and cause the script to crash.
With this information is impossible to occur that you are saying, but I think that your problem is the isset() and not the if() statement. The if statement doesn't change its behaviour depending of the content. Maybe, secureLogout is isset, but is empty, and here you are your possible solution:
if(!empty($_POST['secureLogout'])) {
// empty returns true when is not set, when it's zero values, empty values, etc.
}
See this:
http://php.net/manual/en/function.empty.php
Good luck!
isset return true only if it contains some value. In other words, it returns true only when the variable is not null. Consider that 0 and "" are a value.
You could try to use if(!empty($_POST['secureLogout'])). empty does the same as isset but checks if the variable is empty. 0 and "" are différent than empty. So empty will return false.

Is it possible for a page to know that it has been redirected to using header()?

I'm creating a form. There is some server-side validation being executed in a php file separate from the html file containing the form. If the validation fails, I want to redirect back to the original html page with a error message saying what went wrong.
Right now, I am able to successful validate and redirect back to the html page with header(), but I'm not sure how to create and place the error message. Is it possible to check at the top of the html page with php if it's been redirected to through header()? If so, that would solve the problem...
Thanks!
there are several methods to do this i think.
1 add get parameters like:
<input type="hidden" name="formsent" value="1" />
then add method get to your <form>
when you redirect from the other page,, the get would be in the link so you could send it back
header("Location: http://localhost/yourform.php/?{$_GET['formsent']}");
or you could do the validation in the post
if (isset($_POST) && !empty($_POST)) {
do stuff here.. if all is ok go to next page otherwise show errors
}
or you could add a var into a session using $_SESSION['formsent'] = 1 after the post then u could check that also.
its up 2 u
You should set a variable using PHP sessions.
Form page
session_start();
$_SESSION["formerror"] = "Error code here";
header("Location: http://www.example.com");
Redirected to page
session_start();
$errorcode = $_SESSION["formerror"];
// Now convert the error code to something readable and print it out if needed.
IMO this is much cleaner than a GET variable.
As #Mark wrote, you can send a message in a variable by the url in your header() (I mean url + "?variable=$variable") and capture the message in your page (now php page) by $_GET. The message will depend on your validation
Of course you can check: https://stackoverflow.com/a/872522/2737474 (#Jrgns)
Different ways to pass one variable between pages.
In my opinion, you must be careful in choose one of those:
-If you would use one value for many pages (keeping in mind it would be store on server), it would be better to use SESSION.
-If you would use one value for only two pages, it would be better to use GET or POST (depending on your situation, url/form).
-If you would use one value for many pages and want to keep it between sessions (keeping in mind it would be store on client), it would be better to use COOKIE.
You can do this with using $_GET[] method
If validation is successful then redirect to url like
form.php?status=1 // 1 for success
If validation is failed then redirect to
form.php?status=0 // 0 for fail
In form.php which is your form page.
use simple if-else condition
if(isset($_GET['status']))
{
if($_GET['status']==0)
echo'something went wrong';
//else nothing
}
As many clever users wrote you have several methods how to achive this (I won't write all of these):
1st Use sessions check Daniel's answer
2nd Use GET check Sanket Shembekar's answer
3rd Use rZaaaa's answer, but you can enchant it :D
Enchant:
Page 1
header('error: true');
Page 2
print_r(headers_list()); //and find your error

Redirect to different url depending on request url

I am trying to make a simple redirect php plugin, and i cant get to the bottom, i would really appreciate some help.
Inside a folder i have the php script that will handle the redirect, for ex: /redirect/a.php
Scenario 1:
call /redirect/a.php?key=firstkey the redirect to http://www.url1.com
Scenario 2:
call redirect/a.php?key=secondkey then redirect to http://www.url2.com
General rule:
If a.php is called without key, or with wrong key then display Error.
Thank you!
Use global variable $_GET["key"] to get value of "?key=value", then use header() to redirect.
Note that there cannot be any output before calling header(), that applies even for whitespaces (such as space, or tab).
It could look something like this:
// checking whether the key is sent by user who visits the page
if(!isset($_GET["key]))
{
die("Key is required");
}
// checking whether the key is empty
if(empty($key)
{
die("Key shouldn't be empty");
}
if($_GET["key"] == "firstkey")
{
header("location: http://www.url1.com");
}
It would be better to use array() to list keys that should be accepted by script, you could easily look for them by using in_array().

PHP initializing variables [duplicate]

This question already has answers here:
How to set PHP not to check undefind index for $_GET when E_NOTICE is on?
(3 answers)
Closed 9 years ago.
Im watching several php tutorials, and many of them start something like that:
<?php
if(!$_POST['username']){
..
..
};
?>
or
<?php
$username = $_POST['username'];
..
..
?>
but everytime I visit the page for the first time, it prints an error because $_POST['username'] was not initialize.
How can I I fix this?
thank u
There are four main HTTP methods you can use when making a request to a webserver: GET, POST, DELETE, and PUT. With each of these, you can pass variables with your request that PHP allows you to access. For GET and POST, you can access the variables with the corresponding arrays: $_GET & $_POST, respectively.
If you've never dealt with HTTP methods before, that's because you don't need to really think about it. This is because of the fact that when you type a URL into your browser and hit go, it uses the GET method by default.
So, in this case, what's probably going on is that you're just not making a POST request at all when you load your page. And even if you were, you'd need to have the proper POST variable defined to access it, which in this example would be username.
Since one of the above isn't true, the value of the variable is null. And when you try to access a null value in PHP, the script throws an error (as you've seen). You can change this, but I wouldn't recommend it. I like to know that I'm properly handling my null variables when I write code. But maybe that's just me.
The solution that I recommend, and that others have suggested, is to use the isset() function to see if the variable is defined. This will prevent your code from breaking. Check out the isset documentation for more.
<?php
if(isset($_POST['username'])){
..
..
}
?>
You can set error_reporting to E_ALL ~E_NOTICE
See the documentation on this: http://php.net/manual/en/function.error-reporting.php
or disable it globally in the php.ini file.
use isset($_POST['username'])
if(isset($_POST['username']))
{
.
.
.
}
$_POST['username'] is trying to get the 'username' variable which included the the POST http request to your page (think submitting a form). Normal browsing to a page is an http GET request.
Try using this to avoid issues:
<?php
if(isset($_POST['username'])) {
... if POST and includes a username value, do this ...
} else {
... otherwise do this stuff ...
}
Read me for more info on types HTTP requests: http://techforum4u.com/content.php/229-HTTP-Request-GET-HEAD-POST-PUT-DELETE-OPTION-TRACE
That's because you didn't recieve a value via POST (like from a form post). Add an isset() validation as others are suggesting. Take a look at http://php.net/manual/es/function.isset.php

Editing and creating things with address bar code?

How do I make it so that I can make a thing at the end of the address where the .php is and then tell it to do certain things. For example pull up a page like this:
sampardee.com/index.php?page=whatever
Help?
Anything else I could do with this?
This is generally achieved with the global php array $_GET. You can use it as an associative array to 'get' whatever variable you name in the url. For example your url above:
//this gives the $page variable the value 'whatever'
$page = $_GET['page'];
if($page == 'whatever'){
//do whatever
}
elseif($page == 'somethingelse'){
//do something else
}
Check out the php documentation for more information:
$_GET documentation
and there's a tutorial here:
Tutorial using QUERY_STRING and _GET
A small improvement over Brett's code:
if (array_key_exists('page', $_GET) === false)
{
$_GET['page'] = 'defaultPage';
}
$page = $_GET['page'];
// ... Brett Bender's code here
$_GET is usually used if you are sending the information to another page using the URL.
$_POST is usually used if you are sending the information from a form.
If you ever need to write your code so that it can accept information sent using both methods, you can use $_REQUEST. Make sure you check what information is being sent though, especially if you are using it with a database.
From your question it looks like you are using this to display different content on the page?
Perhaps you want to use something like a switch to allow only certain page names to be used?
i.e.
$pageName=$_REQUEST['page'];
switch($pageName){
case 'home':$include='home.php';break;
case 'about':$include='about.php';break;
case default:$include='error.php';break;
}
include($include);
This is a really simplified example, but unless the $page variable is either home or about, the website will display an error page.
Hope it helps!
I'm not quite sure what you're asking, but I think you're asking how to use GET requests.
Make GET requests against any PHP page as follows:
www.mysite.com/page.php?key1=value1&key2=value2
Now, from within PHP, you'll be able to see key1 -> value1, key2 -> value2.
Access the GET hash from within PHP as follows:
$myVal1 = $_GET['key1'] #resolves to "value1"
$myVal2 = $_GET['key2'] #resolves to "value2"
From here, play with your GET variables as you see fit.
The system of adding page parameters to a URL is know as HTTP GET (as distinct from HTTP POST, and some others less commonly used).
Take a look at this W3 schools page about GET in PHP and ahve a play about in getting parameters and using them in your PHP code.
Have fun!

Categories