Jump to content

$_REQUEST[]


Mememe
 Share

Recommended Posts

Hi.I have a index page called index.php, which I can post information, from page called posting.php This is posting.php

<form method="post" action="index.php"><input type="text" name="title" value="Title of news" style="width: 449px" /><br/><br/><input type="text" name="user" value="Your name" style="width: 449px" /><br/><br/><textarea name="news" cols="50" rows="5" >News in here</textarea><br/><input type="submit" value="Submit news" /></form>

So once the data is entered, it goes to index.php, and in index.php, I have

$curpost   = $_REQUEST['news'];$curpname  = $_REQUEST['user'];$curptitle  = $_REQUEST['title'];

But whenever I load index.php, not post from posting.php, the errors come up:

Notice: Undefined index: news in C:\Program Files\EasyPHP 2.0b1\www\GMScripts Database\index.php on line 120Notice: Undefined index: user in C:\Program Files\EasyPHP 2.0b1\www\GMScripts Database\index.php on line 121Notice: Undefined index: title in C:\Program Files\EasyPHP 2.0b1\www\GMScripts Database\index.php on line 122

It's because no data was received from the posting.php, I think. Is there a way to stop these errors, if I'm not posting?Another thing is with fread(), for the how much bytes to be read, I have filesize('posts.txt') to read it all, but sometimes it comes up with this error

Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\Program Files\EasyPHP 2.0b1\www\GMScripts Database\index.php on line 127

Does anyone know a way to stop this. If anyone needs more info, just ask.Thanks, Mememe :)

Link to comment
Share on other sites

I guess you may want to supply a default value in case no value is provided. You can easily do that like so:

$curpost   = (!empty($_REQUEST['news']) ? $_REQUEST['news'] : 'default value');

Repeated for the three variables and replacing "default value" with the real default value you want of course.You could also do many other things, like redirecting the user to the other page, show an error message saying a value must be provided, etc. etc.... it really depends on the application.As for the fread(), if you just want to get the whole contents, use file_get_contents(). In other cases, as the manual on fread() says, provide the "b" mode parameter as shown in those examples.

Link to comment
Share on other sites

The 'aplication' is basically from the posting.php, I can post news, which is posted on index.php, which is the first page. I'm testing, and I'll report back.[EDIT]About the rb, will it work still if I put it on an web server?

Link to comment
Share on other sites

[EDIT]About the rb, will it work still if I put it on an web server?
If the server is Windows - sure. If it's *nix... I'm assuming it will, but if it doesn't, I guess you'll have to manually make that change or make some sort of test for the OS.
Link to comment
Share on other sites

You can also use isset to check if something is defined before you try to access it. So instead of something like this:

$var = $_POST['name'];

or this:

if ($_POST['name'] == "test")

you could do this:

$var = "";if (isset($_POST['name']))  $var = $_POST['name'];

if (isset($_POST['name']) && $_POST['name'] == "test")

Link to comment
Share on other sites

From the error, I'm getting that your version of PHP doesn't populate the $_REQUEST variable. You may want to use $_POST or $_GET(whichever one your form uses). If you want you can make your own $_REQUEST variable, it's pretty much just

$_REQUEST = array_merge($_GET,$_POST,$_COOKIE);

Personally, I wouldn't use $_REQUEST. I just don't like it quite honestly.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...