Script author: Olsiva
Saving additional fields
     It is shown in this example how to save any additional data about the user with the help of php-scripts. Script creates a form with additional fields for the current user in the page. The user fills in the fields and presses the button “Update”. The transferred data is saved in the file. When the page is visited for the second time, the data is displayed upwards, before the additional fields. It means that the additional fields of each user are hosted at the website. This script may be used to host any data about the user. On its bases users'permission for the website pages can be set. Users can be grouped depending on their interests etc. The source code of this example is shown below.

In the template of the page:

<?IF($USER_ID$)?>
    <div align="center">
        <p>
            <b>This example shows the way of using the script to save additional fields of the current user</b>
        </p>
    </div>
    <hr>
    <?$PHPCODE$("http://phpexample.ucoz.com/php/example001/example001.php?uid=",$USER_ID$)?>
<?ELSE?>
    <div id="phpdiv">
        <p>
            Guests are prohibited from viewing this page. Please, authorize yourself.
        </p>
    </div>
<?ENDIF?>


PHP-script of this example:

<?php
    if(count($_POST)>0)
    {
        $___notjson=1;
        $lines = file('additiondata.dat');
        if ($_POST['f0'] && $_POST['f1'] && $_POST['f2'] && $_POST['f3'] && $_POST['f4'])
        {
            foreach ($lines as $line_num => $line) {
                trim($line);
                list ($id, $data) = split("\t",$line,2);
                if ($id==$_POST['userid'])
                {
                    unset($lines[$line_num]);
                    file_put_contents('additiondata.dat', $lines);
                    break;
                }
            }
            file_put_contents('additiondata.dat', $_POST['userid']. "\t". $_POST['f0']. "|". $_POST['f1']. "|". $_POST['f2']. "|". $_POST['f3']. "|". $_POST['f4']."\n", FILE_APPEND);
            echo "$('#adddata').html('<p> Your car: ".$_POST['f0']." ".$_POST['f1']."<\p><p> Your phone: ".$_POST['f2']." ".$_POST['f3']."<\p><p> The favorite day of the week:".$_POST['f4']."<\p>')";
        }
        else echo "$('#adddata').html('Incorrect data')";
        exit;
    }
    $lines = file('additiondata.dat');
    $status_ok=0;
    foreach ($lines as $line_num => $line) {
        trim($line);
        list ($id, $data) = split("\t",$line,2);
        if ($id==$_GET['uid'])
        {
            list ($a,$b,$c,$d,$e) = split("\|",$data,5);
            echo "<div id='adddata' align='center'><p> Your car: $a $b</p><p>Your phone: $c $d</p><p> The favorite day of the week $e</p></div><hr>";
            $status_ok=1;
            break;
        }
    }
    if (!$status_ok) echo "<div id='adddata' align='center'> There hasn't been any additional data about you yet. Please type it in.</div><hr>";
?>

<p>Additional data:</p>
<form action="/php/example001/example001.php" method="post" onsubmit="_uPostForm(this,{dataType:'script'});return false;" onerror="alert('Error');">
<p><input type="hidden" name="userid" value="<?echo $_GET['uid']?>"></p>
<p> Make of car: </p><input type="text" name="f0" />
<p> Car model: </p><input type="text" name="f1" />
<p> Car producer: </p><input type="text" name="f2" />
<p> Mobile phone model: </p><input type="text" name="f3" />
<p> The favorite day of the week: </p><input type="text" name="f4" />
<p><input type="submit" value="Refresh"></p>
</form>

...Important!

      The usage of the user ID ($USER_ID$ ), sent to the script via GET (from the website page) or POST (from the form on the page) method is not safe and secure. It can be falsified. For safe receive of a real user ID, which views the page with the executed script, use the function ucoz_getinfo("SITEUSERID") in the PHP-script. To do so you need to change the following lines in this example:

1. From: <?$PHPCODE$("http://phpexample.ucoz.net/php/example001/example001.php?uid=",$USER_ID$)?> To: <?$PHPCODE$("http://phpexample.ucoz.net/php/example001/example001.php)?> 2. From: if ($id==$_POST['userid']) To: if ($id==ucoz_getinfo("SITEUSERID") 3. From: file_put_contents('additiondata.dat', $_POST['userid']. "\t". $_POST['f0']. "|". $_POST['f1']. "|". $_POST['f2']. "|". $_POST['f3']. "|". $_POST['f4']."\n", FILE_APPEND); To: file_put_contents('additiondata.dat', ucoz_getinfo("SITEUSERID"). "\t". $_POST['f0']. "|". $_POST['f1']. "|". $_POST['f2']. "|". $_POST['f3']. "|". $_POST['f4']."\n", FILE_APPEND); 4. From: if ($id==$_GET['uid']) To: if ($id==ucoz_getinfo("SITEUSERID")) 5. Delete <p><input type="hidden" name="userid" value="<?echo $_GET['uid']?>"></p>
Date 18.02.2011
Comments:
1 2 »
electronic cigarette Spam 17.08.2014 20:40
21st century electronic cigarette rite aid http://topelectroniccigarettesreviews.com
Gustavowem Spam 15.03.2014 17:58
Gustavowem
buyocfcudi Spam 06.03.2013 09:20
slytherin scarves
forex factory Spam 25.02.2013 12:36
Do you mind if I quote a few of your articles as long as I provide credit and sources back to your website? My blog site is in the exact same area of interest as yours and my visitors would really benefit from a lot of the information you present here. Please let me know if this alright with you. Regards!
ezkaijimeg Spam 24.02.2013 01:50
slytherin scarves
NiliendundCek Spam 22.02.2013 16:14
Test, just a test
owemoRegecawl Spam 09.02.2013 05:40
Test, just a test
lotshesty Spam 02.02.2013 04:33
zaandam
DoeringIT Spam 26.01.2013 09:23
just stopping by to say hey
AdolphusYR Spam 16.01.2013 08:44
simply dropping by to say hey
1-10 11-16
Only registered users can add comments.
[ Registration | Login ]