rasheed
rasheed

Reputation: 1

how to check value of undefined $_POST variable index?

My question is that how can we check value in posted variables in $_POST without defining them as you can see in the code.

if ($_SESSION["Admin"]=="" AND $_REQUEST[act]!="show_login" AND
$_REQUEST[act]!="chk_login" ) {  #not logged in    show_login();   
return;

I am getting these errors,

Upvotes: 0

Views: 855

Answers (3)

LSerni
LSerni

Reputation: 57418

I find it easier on maintenance to check all my variables in a loop:

<?php
    // VARIABLES NEEDED BY THIS PAGE
    $needed_session = array(
         'Admin' => "",
    );
    $needed_request = array(
         'act' => "",
    );
    foreach($needed_session as $var => $default)
        if (!isset($_SESSION[$var]))
            $_SESSION[$var] = $default;
    foreach($needed_request as $var => $default)
        if (!isset($_REQUEST[$var]))
            $_REQUEST[$var] = $default;
?>

Even if writing to _REQUEST is really a bad coding practice, and at least one should differentiate between POST and GET.

As a variation, you might declare only those variables which are known:

    foreach($needed_request as $var => $default)
        if (!isset($_REQUEST[$var]))
            ${$var} = $default;
        else
            ${$var} = $_REQUEST[$var];

There are various possibilities: 1. you might validate syntactically the variables upon import, e.g. through a regexp, too). 2. you might declare some "really really needed variables" whose absence throws out an error page.

If you're going much farther down that road, however, you'd be better advised to investigate some higher level framework.

Upvotes: 0

chris
chris

Reputation: 36947

if(isset($_SESSION))
{
   foreach($_SESSION as $sessKey => $sessVal)
   {
      echo $sessKey . ' - ' . $sessVal.'<br>';
   }
}
echo count($_SESSION);

Now I am not sure if this is exactly what your asking but as I take it you wanna know if a session is set, and if it is, then what is the data in said session. $_SESSION, $_COOKIE, $_POST, $_GET, $_REQUEST are all essentially arrays, so you can treat them like an array when working with them.

Another problem you may be having.. this is cause I noticed your error after posting an answer is you are using xampp, presumably on windows. You may be running into a permissions error with the windows file system, and the sessions not being able to write to the local temp or tmp directory.

Also I don't suggest using $_REQUEST if you don't have to, its a global variable. So if I make a post, get request to your site/software with the name admin, or I set a cookie and call it admin, $_REQUEST will treat it the same as it would any of the specifically defined versions.

Upvotes: 0

nickb
nickb

Reputation: 59709

Use isset() before you try to access the index in the $_REQUEST array, like so:

if( $_SESSION["Admin"] == "" && 
    (!isset( $_REQUEST['act']) || 
        ( $_REQUEST['act'] != "show_login" && $_REQUEST['act'] != "chk_login")))

I think I've added the correct logic that you're looking for.

Upvotes: 2

Related Questions