edit php $_post function code required!
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: edit php $_post function code required!

Hybrid View

  1. #1
    Junior Member
    Join Date
    Jan 2018
    Posts
    9

    edit php $_post function code required!

    hi guys,
    this is my first post

    i have the function:

    PHP Code:
    <?php
       
    if( $_POST["name"]  ) {
          if (
    preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
             
    $_SESSION['TDM_LANG']='it';
             
    TDMRedirect($_SERVER['REQUEST_URI']);

          }
          else {
    $_SESSION['TDM_LANG']='en';
          
    TDMRedirect($_SERVER['REQUEST_URI']);
          }

          exit();
       }
    ?>
    <html>
       <body>

          <form action = "<?php $_PHP_SELF ?>" method = "POST">
             Name: <input type = "text" name = "name" />

             <input type = "submit" />
          </form>

       </body>
    </html>

    but i need to correct it: with this function i change the module language with a form, if the input are numbers i get italian language, if the input are letters i get the english language. I need to modify the condition in a really simple way, cancel the input form and get this:

    italianflag.jpg if i click on this so


    {
    $_SESSION['TDM_LANG']='it';
    TDMRedirect($_SERVER['REQUEST_URI']);

    }


    englishflag.jpg if i click on this so:

    {$_SESSION['TDM_LANG']='en';
    TDMRedirect($_SERVER['REQUEST_URI']);
    }


    what i need is to edit the condition of php and html, getting crazy with this stupid function... thanks in advance!!!
    i took this example that is working on my site but is not suitable to my needs, i simply need the same result when i click on links of two flags

  2. #2
    Junior Member benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    16
    PHP_SELF is vulnerable to an XSS Attack. Remove the action completely.

  3. #3
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Quote Originally Posted by benanamen View Post
    PHP_SELF is vulnerable to an XSS Attack. Remove the action completely.
    ok, thanks for the info could you suggest me the correct solution to make the function work with flags instead of a form?

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    You can still use a form: one with just two <button>s. I'd use a GET, myself.

    Consider also, when you first show the page (and don't have any existing language preference), looking in the request for an "Accept-Language:" header, and basing its initial value on that.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Quote Originally Posted by Weedpacket View Post
    You can still use a form: one with just two <button>s. I'd use a GET, myself.

    Consider also, when you first show the page (and don't have any existing language preference), looking in the request for an "Accept-Language:" header, and basing its initial value on that.
    thanks for the suggestion, coud you make me an example with the code that i posted?

  6. #6
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Edit, i modified the code in this way and it's working:

    <?php
    if(isset($_POST['button1'])) {
    $_SESSION['TDM_LANG']='it';
    TDMRedirect($_SERVER['REQUEST_URI']);

    }

    if(isset($_POST['button2'])) {
    $_SESSION['TDM_LANG']='en';
    TDMRedirect($_SERVER['REQUEST_URI']);

    }

    ?>
    <html>
    <body>

    <form method = "POST" action=''>


    <input type="submit" name="button1" value="italian">
    <input type="submit" name="button2" value="English">

    </form>

    </body>
    </html>



    do you know how to set the default language with "Accept-Language:" ??
    any code integration will be really appreciated!

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Running Gear"
    Posts
    22,573
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  8. #8
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    i gave a look at the links and i just need your confirmation regarding this way to set the session language:

    $_SESSION['TDM_LANG'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);

    in this way i think that i'm setting the language according to the visitor preferences, but is there a simple way to set the DEFAULT LANGUAGE always italian ?

  9. #9
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,911
    Maybe something like this?
    PHP Code:
    $_SESSION['TDM_LANG'] = 'italian';
    if(!empty(
    _SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        
    $_SESSION['TDM_LANG'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  10. #10
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Quote Originally Posted by NogDog View Post
    Maybe something like this?
    PHP Code:
    $_SESSION['TDM_LANG'] = 'italian';
    if(!empty(
    _SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        
    $_SESSION['TDM_LANG'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);

    tried it but there is some errrors, blank page...

  11. #11
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,546
    There's a real $imple $yntax error in there ... look closely. And don't yell @Weedpacket for a $imple typo ... let the rest of us do that instead!
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  12. #12
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Quote Originally Posted by dalecosp View Post
    There's a real $imple $yntax error in there ... look closely. And don't yell @Weedpacket for a $imple typo ... let the rest of us do that instead!
    Thanks, i corrected the _SERVER error in $_SERVER but if i add that code i have the following situation:

    first loaded page in italian, any link after that is automatically switch the session in english so i have to select manually italian for any link

  13. #13
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,911
    So...I think maybe what you are after is setting it only if not already set?
    PHP Code:
    if(empty($_SESSION['TDM_Language'])) {
      
    $_SESSION['TDM_LANG'] = 'italian';
      if(!empty(
    $_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        
    $_SESSION['TDM_LANG'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
      }  

    Then if is set elsewhere, such as by the code that handles the click of a language flag, it won't try to reset it as long as the session is active.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  14. #14
    Junior Member
    Join Date
    Jan 2018
    Posts
    9
    Quote Originally Posted by NogDog View Post
    So...I think maybe what you are after is setting it only if not already set?
    PHP Code:
    if(empty($_SESSION['TDM_Language'])) {
      
    $_SESSION['TDM_LANG'] = 'italian';
      if(!empty(
    $_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
        
    $_SESSION['TDM_LANG'] = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
      }  

    Then if is set elsewhere, such as by the code that handles the click of a language flag, it won't try to reset it as long as the session is active.
    what i need is: set ITALIAN language as default language if is not already setted a second language

    should the function return this?

  15. #15
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,911
    Well, I used the wrong array key in the first line, but the intention was:
    • If the "TDM_LANG" session value already exists, use that. (Presumably the user already selected explicitly or via HTTP header.)
    • If not, but the browser sent an "Accept-Language" HTTP header, use that.
    • Otherwise, set it to "italian"
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •