$_SERVER["PHP_SELF"] Apache/Xampp forbidden access - php

I am using an Xampp/Apache server on localhost.
I am trying to use an action on a form which posts to "register.php".
<form method="post" action= "<?php echo htmlspecialchars($_SERVER["register.php"]);?>">
I get a 403 error "Access Forbidden".
In the url it states:
<br%20/><b>Notice</b>:%20%20Undefined%20index:%20register.php%20in%20<b>C:/xampp/htdocs/2001/create_account.php</b>%20on%20line%20<b>45</b><br%20/>
(Error on line 45 which is the line of code above).
The file this code is in is a php file, create_account.php
I have tried changing apache httpd.conf permissions, no changes. I have tried finding the solution elsewhere but no luck with a fix.

You will need to read the manual page for $_SERVER carefully
http://www.php.net/manual/en/reserved.variables.server.php
you may find action="register.php" is all you will need

In your code example you've tried to call for an undefined key which array $_SERVER doesn't have and therefor you've got an error.
$_SERVER is a php superglobal and it's form is an array. Arrays can be indexed, associative or mutlidimensional. Associative arrays use keys to accesses values ('key' => 'value'). Example of an associative array:
$person=array('Name' => 'John', 'Last name' => 'Spencer', 'Age' => 22);
In order to get John's last name you'd first need to specify the array in which all of the information is being held ($person) and then request certain information from that array via key (['Last name']). Example of such request:
$lastName=$person['Last name'];
If you already know the page you'd like to accesses you should type it in HTML as plain text (method="register.php"), there's no need for any php functionality in doing this. Function htmlspecialchars() also isn't necessary since you are the one typing and there's no need to sanitize your own input.
Better than plain text however would be to use $_SERVER['PHP_SELF'] which returns relative path to the page you're currently on. This way is better than previous since if at some point you were to rename your file you wouldn't have to change to your form's method value unlike in previous example.
Finally if you're not leaving the page you're currently on you could also place an empty string as method (method="").
Recommended for further read:
php.net's documentation on arrays
w3school's documentation on arrays

Related

Is it possible to send an array in a php header (like you can with a form)?

I am trying to make a dynamically sized form for a web-page I am creating! I have had no issue passing the information needed to the 'action' page through a form (including two arrays), by setting the name of all dynamically created forms to be name[i].
To get the data from the array in the 'action' file, I use the code below, and it works fine:
$_POST['name'][$i]
However, I wish to return the information to the form if there is an error with any of it, and the way I am doing this is with headers.
header("Location: ../originalPage.php?error=error&someValue=".$someValue."&someArray[]=".$someArray);
exit();
Is there anything I need to change for this to return something other than Array()?
Clearly the header is using the $_GET method rather than the form's $_POST method, but why can I only send the array one way?!
Any help would be appreciated!
The issue you have is that you try to concatenate your array to a string, but that does not happen in the way you would prefer. You could convert your array into JSON, like this:
../originalPage.php?error=error&someValue=".$someValue."&someArray[]=".json_encode($someArray));
Read more about json_encode by clicking on the link.

PHP Warning while passing $_POST to array_key_exists()

So I am using the following style of code if(array_key_exists('some_value', $_POST)){echo 'hi';}
For PHP 5.2.17 I am getting a warning from this style of code. This is the warning:
WARNING: argument 2 for array_key_exists() is not either an array or an object on line: 123
This seems strange to me because I believe that the $_POST array should always be defined. Is that not the case? I'm not sure what would cause the $_POST array to not be considered an array. I am not resetting $_POST to anything so it should exist as an array at all times. Does anyone have any idea what is wrong. Please let me know if more information is needed and thank you for the help.
Edit: I should note that this only happens on the production server. My local environment does not have this problem.
The Superglobals $_POST and $_GET are only populated if the script is POSTed to or GET from. In your example, the reason that you'd get that error is if there was not post action to the script. Before checking for a certain post value, you should check to make sure there was a post:
if(isset($_POST)) {
//The form was posted
}
In that fashion. From there, you can check for certain values using array_key_exist, or you can further check isset($_POST['myKey']).
Use if(isset($_POST['some_value'])) { echo 'hi'; } instead. Never had a problem with it.
Also check if you are not overriding or unsetting $_POST (or some framework you are using is doing it for you). I avoid to do so with superglobal variables since I think it is a bad practice and might give headaches like this one.

file_exists() expects parameter 1 to be a valid path, string given

I'm designing a web application that can be customized based on which retail location the end user is coming from. For example, if a user is coming from a store called Farmer's Market, there may be customized content or extra links available to that user, specific to that particular store. file_exists() is used to determine if there are any customized portions of the page that need to be imported.
Up until now, we've been using a relatively insecure method, in which the item ID# and the store are simply passed in as GET parameters, and the system knows to apply them to each of the links within the page. However, we're switching to a reversible hash method, in which the store and item number are encrypted (to look something like "gd651hd8h41dg0h81"), and the pages simply decode them and assign the store and ID variables.
Since then, however, we've been running into an error that Googling extensively hasn't found me an answer for. There are several similar blocks of code, but they all look something like this:
$buttons_first = "../stores/" . $store . "/buttons_first.php";
if(file_exists($buttons_first))
{
include($buttons_first);
}
(The /stores/ directory is actually in the directory above the working one, hence the ../)
Fairly straightforward. But despite working fine when a regular ID and store is passed in, using the encrypted ID throws this error for each one of those similar statements:
Warning: file_exists() expects parameter 1 to be a valid path, string given in [url removed] on line 11
I've had the script spit back the full URL, and it appears to be assigning $store correctly. I'm running PHP 5.4.11 on 1&1 hosting (because I know they have some abnormalities in the way their servers work), if that helps any.
I got the same error before but I don't know if this solution of mine works on your problem you need to remove the "\0" try replace it:
$cleaned = strval(str_replace("\0", "", $buttons_first));
it worked on my case.
Run a var_dump(strpos($buttons_first,"\0")), this warning could come up when a path has a null byte, for security reasons. If that doesn't work, check the length of the string and make sure it is what you'd expect, just in case there are other invisible bytes.
It may be a problem with the path as it depends where you are running the script from. It's safer to use absolute paths. To get the path to the directory in which the current script is executing, you can use dirname(__FILE__).
Add / before stores/, you are better off using absolute paths.
I know this post was created on 2013 but didn't saw the common solution.
This error occurs after adding multiple to the file submit form
for example you are using files like this on php: $_FILES['file']['tmp_name']
But after the adding multiple option to the form. Your input name became file => file[]
so even if you post just one file, $_FILES['file']['tmp_name'] should be change to $_FILES['file']['tmp_name'][0]

debug php code of a facebook app present on heroku

At first apologies in advance if there is some problem in my ques, i am new to heroku ,my basic problem is i have some messed up code, where i want to test if i am able to fetch facebook variables in my own code and use them..
in my .php file i want to put name for person using my facebook app to displayname array variable of array and url of array should get the application users picture..i took the idea to assign these value via the index.php file provided by facebook itself.
My .php file code is :-
$basic = $facebook->api('/me');
$options = array(
'displayName' => he(idx($basic, 'name')),
'image' => array(
'url' => 'https://graph.facebook.com/'.he($id).'/picture?type=square',
'height => '48',
'width => '48'
)
);
but there is something wrong going here which i cant figure out.
i tried to debug it via javascript or other techniques but now able to connect the .php file to some .js file by any means to transfer variable values present in .php file and print them on my browser,i use to edit code at my own system and push it via git and since the code is executed at heroku i cant figure out what errors are creping in..i am using free account as per now so is there any way i can see my code in execution at heroku.. or any help to debug my code efficiently..
Edit1: alternatively is there any way i can pass these variable from my .PHP file to a separate .JS file and print variables in message box or something..any example code given will help a lot..there are many questions asked in this regard to transfer variables from separate .PHP file to separate .JS file..but i found no direct answer for it, all suggest workarounds but no direct way... questions i visited for it are ..
What's the best way to pass a PHP variable to Javascript?
Grab/input php variable in javascript?
and some more but dint find the perfect answer.
Edit2: if my ques needs more info plz let me know,and if second option is the choice left to debug my code ..then can someone give me an example with transferring variable/array present in testfile.php file say present at appfolder/php/lib/testfile.php and output it on browser in HTML format using testjs.js file say present at appfolder/lib/js/testjs.js
A common exchange format between PHP (or other language) and JavaScript is JSON. You can encode an array (or an php object) to json using json_encode, in PHP. Like this :
$options_json = json_encode($options);
So, you can write this javascript variable in your html results, like this :
echo '<script>var options = ', json_encode($options), ';</script>';
Your picture will then be accessible using javascript :
console.log(options);
console.log(options.url);

accessing array from another page in php

I would like to know how can I access or read the array from another page. I am working on a PHP page which contains the array, and I want to display the content of that array on another PHP page.
For example, I used the following method in a PHP file and I want to get the content of the array in another PHP file. what is the method that is going to receive the array's content in the second page.
<?php
$r = new HttpRequest('http://localhost/sameh.php', HttpRequest::METH_POST);
$r->addPostFields(array("n" => 'heba')) ;
$r->send();
?>
This code is in the first page but I don't know what to write to receive it on the second one.
Maybe my question was not that clear and sorry about that ,, I want to find a way to access the array that is defined inside the HttpRequest() class on another page. So that the array "n" that include value "heba" will be displayed on another.php page. this is what make me thinking that the problem is on how to access the content of the array on the second page.
I tried the session and It sends the array to another page ,, but when I tested with the
httpRequest() method it doesn't send the content of the array "heba" to the second page.
Thanks for your help.
Sounds like this is a job for Sessions.
You can read the complete session guide here
In the script that has the array you can do something like:
session_start();
$_SESSION['array'] = $array;
In the next script you access it similarly:
session_start();
print_r($_SESSION['array']);
Include that file in your php file where you want to use that array. This should solve your issue PHP - How to send an array to another page?
I'm not sure what the HttpRequest class is, but at a guess, it's POSTing variables to the sameh.php file. You should be able to access the variable on the next page by doing this:
echo $_POST['n'];
Which should print "heba".
Note that to work with serialized arrays, you need to use POST as the form's transmission method, as GET has a size limit somewhere around 1024 characters.
I'd use SESSIONs wherever possible.
You can use serialize() and unserialize() on your array to represent it as a string and pass it via POST.

Categories