dimanche 29 mars 2020

Condition-statements aren't working,What's wrong with this php code?

With the help of this php function, users can create a blank page on my website.But it has some problems that I failed to solve. In brief they are,

  1. In this code,it shows three undifined index on page load & submit, they appear in php logs (on submit in page if showing error is on) :
if (isset($_POST['oke'])) {
    $plink = permalink();
    $plink2 = permalink('ya');
    $nama = preg_replace("/(\W)/",'-',strtolower(trim($_POST['nama'])));
    $link = $nama;
    $posisi = strip_tags($_POST['posisi']);
    $div=strip_tags($_POST['div']);
    $hak=strip_tags($_POST['hak']); 
    if (empty($link) or preg_match("/([0-9]+)|index|header|footer/",$link)) $error = 'Invalid name only a-z';
    if (array_key_exists($link,$plink2)) $error = 'Name already exists';

The error is


'php' log:
[29-Mar-2020 05:00:10 America/Detroit] PHP Notice:  Undefined index: nama in /storage/emulated/0/data_files/www/manage/phone/editpanel/page.php on line 11
[29-Mar-2020 05:00:10 America/Detroit] PHP Notice:  Undefined index: posisi in /storage/emulated/0/data_files/www/manage/phone/editpanel/page.php on line 13
[29-Mar-2020 05:00:10 America/Detroit] PHP Notice:  Undefined index: div in /storage/emulated/0/data_files/www/manage/phone/editpanel/page.php on line 14
[29-Mar-2020 05:00:10 America/Detroit] PHP Notice:  Undefined index: hak in /storage/emulated/0/data_files/www/manage/phone/editpanel/page.php on line 15

2.if a page with given name already exists then it should show warning that the page already exists, but any name you enter no matter they exist or not warning is showing!! What should I do?? I need your help to correct this code, And thanks in advance. Page.php

<?php
defined('NBER') or die ('Acess Error');
echo '<div id="right">';
menu_atas("Add Page");
if (isset($_POST['oke'])) {
    $plink = permalink();
    $plink2 = permalink('ya');
    $nama = preg_replace("/(\W)/",'-',strtolower(trim($_POST['nama'])));
    $link = $nama;
    $posisi = strip_tags($_POST['posisi']);
    $div=strip_tags($_POST['div']);
    $hak=strip_tags($_POST['hak']); 
    if (empty($link) or preg_match("/([0-9]+)|index|header|footer/",$link)) $error = 'Invalid name only a-z';
    if (array_key_exists($link,$plink2)) $error = 'Name already exists';
    if (!$error) {
    //get newfile//
    $data="data/$user_nama/page";
    $opendir=opendir($data);
    while(false !== ($file=readdir($opendir))) {
    if ($file == "." or $file == ".." or preg_match("/([a-z]+)/",str_replace('.php','',$file))) continue;
    $pfile []= str_replace('.php','',$file);
    }
    $lastfile= max($pfile);
    $nextfile=$lastfile + 1;
    $newfile=$nextfile.".php";
    //create a new page file//
    $open=fopen("$data/$newfile","w");
    fwrite($open,"");
    //write content to the page//
    $d = "data/$user_nama/alat/permalink.dat";
        $o1 = fopen($d,'r');
        $last_konten = fread($o1,filesize($d));
        $nl = $nextfile.'='.$link;
        $konten = trim($last_konten.','.$nl,',');
        $o2 = fopen($d,'w');
        $sukses = fwrite($o2,$konten);
    if ($sukses) header('location: ?act=pagelist');
    }
} 
$error = (@$error) ? '<div class="error">'.@$error.'</div>' : '';
echo '
'.@$error.'
<form action="" method="post">
Page name:<br/>
<input type ="text" name="example-page"/><br/>
<input type ="submit" name="oke" value="Add New Page"/><br/>
</form>
';
menu_bawah();
echo '</div>';
?>

NOTE: if I change input name="example-page" to link or nama any changes are not taking place!

Aucun commentaire:

Enregistrer un commentaire