Jump to content

Recommended Posts

Hi again fellas.
Ill make it quick.

I attached photo and here is the code. I am braking my head about 2 hours on how to fix those errors and bumped into dead end :s

 

 

 

<form method="get">
Tekstą:<br>
<textarea name="cont" rows="5" cols="60"></textarea>
<br>Įrašyti į failą: <input type="text" name="text" value="">.txt
<br><input type="radio" name="1" value="1">1 
    <input type="radio" name="2" value="2">2
    <input type="radio" name="3" value="3">3 kartus
<br><input type="submit" name="4" value="Įrašyti">
</form>
 

<?php

$file=$_GET['text'];
$vk=fopen($_SERVER['DOCUMENT_ROOT']."/".$file.".txt","wb"); // pasiremta http://stackoverflow.com/questions/9265274/php-create-and-save-a-txt-file-to-root-directory
 
if ($_GET['1'])
{   fwrite($vk,$_GET['cont']);
     fclose($vk);
      echo "<br>Failas "."<b>".$file."</b>".".txt papildytas tekstu: "."[<b>".$_GET['cont']."] "."1"."</b>"." kart.";}
elseif ($_GET['2'])
{   fwrite($vk,$_GET['cont']);
    fwrite($vk," ".$_GET['cont']);
     fclose($vk);
       echo "<br>Failas "."<b>".$file."</b>".".txt papildytas tekstu: "."[<b>".$_GET['cont']."</b>] "."2"."</b>"." kart.";}
elseif ($_GET['3']) 
{   fwrite($vk, $_GET['cont']);
     fwrite($vk," ".$_GET['cont']);
      fwrite($vk," ".$_GET['cont']);
       fclose($vk);
        echo "<br>Failas "."<b>".$file."</b>".".txt papildytas tekstu: "."[<b>".$_GET['cont']."</b>] "."3"."</b>"." kart.";}
 
?>

I would really apreaciate your help fellow programers :D not noob as me ofc

post-203097-0-06988900-1481483913_thumb.png

Edited by HaxorNab
Link to comment
https://forums.phpfreaks.com/topic/302716-undefined-offset/
Share on other sites

You are not doing any checks to see if the form has been submitted so the entire code runs before you doing anything. Look up and learn about isset.

 

There are other problems I am sure others will tell you about.

Edited by benanamen
Link to comment
https://forums.phpfreaks.com/topic/302716-undefined-offset/#findComment-1540208
Share on other sites

Yes. I already found my mistake. Thanks for trying to help :) 

I made same radio names and added  if ($_GET['1']  == (radio value)) and its all perfect

 

That is still wrong. $_GET can have numerous parameters. You are missing a name such as id=1. In your case it is name=1. You should probably be using POST instead of GET.

<?php
if (isset($_GET['name']))
    {
    if ($_GET['name'] == 1)
        {
        //do something
        }

    if ($_GET['name'] == 2)
        {
        //do something
        }

    if ($_GET['name'] == 3)
        {
        //do something
        }
    }
?>
Edited by benanamen
Link to comment
https://forums.phpfreaks.com/topic/302716-undefined-offset/#findComment-1540214
Share on other sites

More importantly, you need to start thinking about security and robustness. Websites are public, which means you'll get all kinds of requests from all kinds of sources. Not all of them are valid and friendly. There will be invalid requests, malformed input, automated attacks and maybe even targeted attacks. Your application has to survive those.

 

Right now, the script has no security or robustness whatsoever. Anybody can write arbitrary data to arbitrary files without any validation. They can upload malware, overwrite existing files and even leave the document root to screw up your server (nothing prevents them from using “../” in the filename). On top of that, the code is wide open to cross-site scripting attacks.

 

Programming is a lot more than making error messages go away. You need to actually think about what you're doing (yes, even as a newbie). In your specific case, I recommend you forget about creating files on your server. It's simply too dangerous as long as you don't understand the implications. You should store the text in a database instead. I'm sure you have one installed already.

Link to comment
https://forums.phpfreaks.com/topic/302716-undefined-offset/#findComment-1540218
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.