Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Change username  (Read 13864 times)

0 Members and 1 Guest are viewing this topic.

Rob Pomeroy



    Prodigy

  • Systems Architect
  • Thanked: 124
    • Me
  • Experience: Expert
  • OS: Other
Re: Change username
« Reply #15 on: March 03, 2006, 05:09:15 AM »
Okay.  In that case, if we don't particularly care what the contents of username.txt is, we can simply replace it everytime with a file containing the new details.  You can put this all in one script if you like.  The following script does the job, but it would not produce perfect results for names that contain an apostrophe.

Code: [Select]
<?php
// Grab POST variables and prefix with p_
//
import_request_variables('p''p_');

// A username variable for simplicity in the code
$namefile 'username.txt';

?>

<html>
<head>
  <title>Testing username function</title>
</head>
<body>

<?php

if(isset($p_username))
{
  
// We have been sent a username variable, so change the contents of username.txt, and advise the user
  
  // Write the username - this overwrites the contents of the file if it already exists
  
file_put_contents ($namefile$p_username);
  
  
// We're going to be echoing the username back next, so need to do a little sanity check:
  
$p_username htmlentities($p_username);
  
  
// Confirm
?>

<p>
  Thank you, <?php echo $p_username?>.  Your user name has
  been updated.
</p>
<?php

} else {
  
// No user name is set; the form has not been used

  // Grab the user name from the file, if it exists; if not, set it to '-undefined-'
  
if (file_exists($namefile))
  {
    
$username file_get_contents($namefile);
  } else {
    
$username '-undefined-';
  }

  
// Again, we're going to be echoing the username back next, so do the sanity check:
  
$username htmlentities($username);

  
// Display a form to enable name changing
?>

<p>
  Welcome.  The username is currently <?php echo $username?>.
  Use the form below to change this.
</p>
<form method='POST'>
  Username: <input type='text' name='username'>
  <input type='submit' value='Go!'>
</form>
<?php
  
}

?>

</body>
</html>
Only able to visit the forums sporadically, sorry.

Geek & Dummy - honest news, reviews and howtos

Blackberry

    Topic Starter


    Adviser
  • For those with wings, fly to your dreams.
    Re: Change username
    « Reply #16 on: March 03, 2006, 06:48:14 AM »
    Could it be that there is a fault in it, because when I type the name, and I click on go, I see a white screen, and the text file istn't changed....
    Everybody knows you can't click here. But I know you will try it :)

    Rob Pomeroy



      Prodigy

    • Systems Architect
    • Thanked: 124
      • Me
    • Experience: Expert
    • OS: Other
    Re: Change username
    « Reply #17 on: March 03, 2006, 07:34:51 AM »
    I've tested this on my server and it works fine.  What version of php are you using?  (Do phpinfo(); in any script.)  Can you get to php.ini?  If so, switch display_errors on and reload the web server.
    « Last Edit: March 03, 2006, 07:38:45 AM by robpomeroy »
    Only able to visit the forums sporadically, sorry.

    Geek & Dummy - honest news, reviews and howtos

    Blackberry

      Topic Starter


      Adviser
    • For those with wings, fly to your dreams.
      Re: Change username
      « Reply #18 on: March 03, 2006, 07:52:10 AM »
      I have php version 4.4, does this script works under that
      Everybody knows you can't click here. But I know you will try it :)

      Rob Pomeroy



        Prodigy

      • Systems Architect
      • Thanked: 124
        • Me
      • Experience: Expert
      • OS: Other
      Re: Change username
      « Reply #19 on: March 03, 2006, 08:02:58 AM »
      Sorry - file_put_contents only works under PHP 5+.  I have rewritten the script.  Can you answer my other question about php.ini?  That would really help with debugging.

      Code: [Select]
      <?php
      // Grab POST variables and prefix with p_
      //
      import_request_variables('p''p_');

      // A username variable for simplicity in the code
      $namefile 'username.txt';

      ?>

      <html>
      <head>
        <title>Testing username function</title>
      </head>
      <body>

      <?php

      if(isset($p_username))
      {
        
      // We have been sent a username variable, so change the contents of username.txt, and advise the user
        
        // Write the username - this overwrites the contents of the file if it already exists
        
      $filehandle fopen($namefile'w');
        
      fwrite($filehandle$p_username);
        
      fclose($filehandle);
        
        
      // We're going to be echoing the username back next, so need to do a little sanity check:
        
      $p_username htmlentities($p_username);
        
        
      // Confirm
      ?>

      <p>
        Thank you, <?php echo $p_username?>.  Your user name has
        been updated.
      </p>
      <?php

      } else {
        
      // No user name is set; the form has not been used

        // Grab the user name from the file, if it exists; if not, set it to '-undefined-'
        
      if (file_exists($namefile))
        {
          
      $username file_get_contents($namefile);
        } else {
          
      $username '-undefined-';
        }

        
      // Again, we're going to be echoing the username back next, so do the sanity check:
        
      $username htmlentities($username);

        
      // Display a form to enable name changing
      ?>

      <p>
        Welcome.  The username is currently <?php echo $username?>.
        Use the form below to change this.
      </p>
      <form method='POST'>
        Username: <input type='text' name='username'>
        <input type='submit' value='Go!'>
      </form>
      <?php
        
      }

      ?>

      </body>
      </html>
      Only able to visit the forums sporadically, sorry.

      Geek & Dummy - honest news, reviews and howtos

      Blackberry

        Topic Starter


        Adviser
      • For those with wings, fly to your dreams.
        Re: Change username
        « Reply #20 on: March 03, 2006, 08:08:41 AM »
        dude.... your my hero  :D. Finaly I have a script that works! I really thank you!!!!. About that php.ini, I've found the file, and I edited it, and now it shows any error. But again, a big thanks for helping me out.

        p.s. Do you know how I read the text file now  :)
        Everybody knows you can't click here. But I know you will try it :)

        Rob Pomeroy



          Prodigy

        • Systems Architect
        • Thanked: 124
          • Me
        • Experience: Expert
        • OS: Other
        Re: Change username
        « Reply #21 on: March 03, 2006, 08:13:41 AM »
        Quote
        I really thank you!!!!
        My pleasure!

        Quote
        About that php.ini, I've found the file, and I edited it, and now it shows any error.
        You will find that a BIG help whilst you're coding.  It can be a slight security risk on a production server, but you should be fine to leave it as it is for now.

        Quote
        p.s. Do you know how I read the text file now  :)
        Not sure what you're asking there?
        Only able to visit the forums sporadically, sorry.

        Geek & Dummy - honest news, reviews and howtos

        Blackberry

          Topic Starter


          Adviser
        • For those with wings, fly to your dreams.
          Re: Change username
          « Reply #22 on: March 03, 2006, 08:45:51 AM »
          I've found my answer, you could simply read it with: <?php
                                                                                       include("username.txt");
                                                                                       ?>

          Everybody knows you can't click here. But I know you will try it :)

          Rob Pomeroy



            Prodigy

          • Systems Architect
          • Thanked: 124
            • Me
          • Experience: Expert
          • OS: Other
          Re: Change username
          « Reply #23 on: March 03, 2006, 02:49:21 PM »
          Not advisable.  Learn from the pointers in my script.  It is possible for people to use any characters at all in the place of "username".  If they are clever, they could insert php code into the username.txt file.  If you unguardedly include() that in your php script, you make it possible for them to remotely execute code on your server.  Not a good plan.

          ALWAYS DISTRUST POST/GET VARIABLES.  If you are using the contents, safely parse them, for example by using htmlentities().  Read the PHP manual pages for more details.

          include() will parse and execute the contents of the text file, as if it were HTML/PHP code.  If you wish to display the contents of the text file, use file_get_contents().

          Read my script over, and make sure you understand not only how it all works, but why I have put those safeguards in place.
          Only able to visit the forums sporadically, sorry.

          Geek & Dummy - honest news, reviews and howtos