Postingan lainnya
Kelas Premium!
Belajar bikin website dari nol sekarang
Gunakan kupon "lebihcepat" untuk diskon 25%!
[SOLVED] bermasalah memanggil function yg berada di class [/SOLVED]
func.php
<?php
class func extends db{
private $table = 'person';
private $pengguna = 'users';
public function __construct()
{
parent::__construct();
}
public function insert($pemakai,$fields = array())
{
//ambil kolom
$column = implode(",", array_keys($fields));
//ambil nilai
$valueArrays = array();
$i = 0;
foreach ($fields as $key=>$values) {
if( is_int($values) ){
$valueArrays[$i] = $this->escape($values) ;
}else{
$valueArrays[$i] = "" . $this->escape($values) . "";
$i++;}
}
$values = implode(",", $valueArrays);
$query = "INSERT INTO $pemakai ($column) VALUES ($values)";
return $this->run_query($query, 'have problem when you input data');
}
public function get_info( $pemakai,$column, $value)
{
if( !is_int($value) )
$query = "SELECT * FROM $pemakai WHERE $column = '$value' AND ((count <= 3) OR (count >= 3 AND expired_block < NOW()))";
$result = $this->_db->prepare($query);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
return $row;
}
}
public function run_query($query, $msg){
if($this->_db->query($query)) return true;
else die($msg);
}
public function escape($name){
return $this->_db->quote($name);
}
public function register_user($fields = array())
{
if( $this->insert($this->pengguna,$fields) ) return true;
else return false;
}
public function login_user($username , $password)
{
$data = $this->get_info($this->pengguna, 'username', $username);
if(password_verify(input::get('password'), $data['password']) )
return true;
else return false;
}
public function cek_nama($username)
{
$data = $this->get_info($this->pengguna, 'username', $username);
if(empty($data)) return false;
else return true;
}
public function filterinjection($value){
// Karakter yang sering digunakan untuk sqlInjection
$char = array ('-','/','\\',',','.','#',':',';','\'','"',"'",'[',']','{','}',')','(','|','`','~','!','@','%','$','^','&','*','=','?','+');
// Hilangkan karakter yang telah disebutkan di array $char
$cleanval = str_replace($char, '', trim($value));
return $cleanval;
}
public function read(){
$query = "SELECT * FROM $this->table";
$result = $this->_db->prepare($query);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC))
$article[]=$row;
rsort($article);
return $article;
}
public function view(){
$query = "SELECT * FROM $this->table";
$result = $this->_db->prepare($query);
$result->execute();
}
public function post( $value)
{
$value = "'" . $value . "'";
$query = "SELECT * FROM $this->table WHERE url=$value";
$result = $this->_db->prepare($query);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC))
return $row;
}
public function cari( $value, $column, $column2)
{
$value = "'%" . $value . "%'";
$query = "SELECT * FROM $this->table WHERE $column LIKE $value OR $column2 LIKE $value";
$result = $this->_db->prepare($query);
$result->execute();
while($row = $result->fetch(PDO::FETCH_ASSOC))
$hasil[]=$row;
return $hasil;
}
public function input($fields = array()){
if( $this->insert($this->table,$fields) ) return true;
else return false;
}
public function delete($pemakai,$fields = array()){
//ambil kolom
$column = implode(",", array_keys($fields));
//ambil nilai
$valueArrays = array();
$i = 0;
foreach ($fields as $key=>$values) {
if( is_int($values) ){
$valueArrays[$i] = $values ;
}else{
$valueArrays[$i] = $this->escape($values);
$i++;}
}
$values = implode(",", $valueArrays);
$query = "DELETE FROM $pemakai WHERE $column=$values";
return $this->run_query($query, 'have problem when you delete data');
}
public function hapus($fields = array()){
if( $this->delete($this->table,$fields) ) return true;
else return false;
}
public function update($nama,$alamat,$gambar,$id,$author){
$query = "UPDATE $this->table SET nama='$nama', alamat='$alamat', gambar='$gambar',author='$author' WHERE id='$id'";
return $this->run_query($query, 'have problem when you delete data');
}
public function resetlog($username){
$bancheck = "UPDATE $this->pengguna SET expired_block = '0000-00-00 00:00:00',count = '0' WHERE username='$username'";
return $this->run_query($bancheck, 'Salah input');
}
public function updatelog($username){
$this->updatecount($username);
$this->updateblockexpired($username);
}
public function updatecount($username){
$bancheck = "UPDATE $this->pengguna SET count = count+1 WHERE username='$username'";
return $this->run_query($bancheck, 'Salah input');
}
public function updateblockexpired($username){
date_default_timezone_set('Asia/Jakarta');
$starttime = date("Y-m-d h:i:s", strtotime("+3 minutes"));
$bancheck = "UPDATE $this->pengguna SET expired_block = '$starttime' WHERE username='$username' AND count >= 3";
return $this->run_query($bancheck, 'Salah input');
}
public function update2($id,$nama,$alamat,$author){
$query = "UPDATE $this->table SET nama='$nama', alamat='$alamat',author='$author' WHERE id='$id'";
$result = $this->_db->prepare($query);
$result->execute();
}
/*Installer*/
//membuat database
public function createdatabase($konek2,$databases){
mysqli_query($konek2,"create database ".$databases."") or die('NAMA HOSTING,USERNAME DAN PASSWORD DI HOSTING HARUS BENAR');
echo "└ konek ke server... ✓<br/>";
echo "└ berhasil membuat database! ✓<br/>";}
//membuka database yang telah dibuat
public function opendatabase($konek2,$databases){
mysqli_select_db($konek2,$databases);
echo "└ database telah ditemukan... ✓<br/>";}
//membuat table user dengan user standar
public function createtable($konek2){
mysqli_query($konek2,"create table users(username varchar(30) primary key not null, password varchar(32) not null)");
echo "└ berhasil membuat table! ✓<br/>";}
}
?>
template.php
<?php
class template{
function template_create($path, $vars) {
global $url_base, $url_home;
extract($vars);
require 'templates/' . trim($path, '/') . '.php';
exit;
}
}
route.php
<?php
class route{
function route_add($patterns, $callback) {
$routes = array();
global $routes;
if( ! is_array($patterns)) {
$patterns = array($patterns);
}
foreach($patterns as $pattern) {
$pattern = trim($pattern, '/');
$pattern = str_replace(
array(
'\(',
'\)',
'\|',
'\:any',
'\:num',
'\:all',
'#'
),
array(
'(',
')',
'|',
'[^/]+',
'\d+',
'.*?',
'\#'
),
preg_quote($pattern, '/'));
$routes['#^' . $pattern . '$#'] = $callback;
}
}
function route_execute() {
global $routes;
$url = $_SERVER['REQUEST_URI'];
$base = str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME']));
if(strpos($url, $base) === 0) {
$url = substr($url, strlen($base));
}
$url = trim($url, '/');
foreach($routes as $pattern => $callback) {
if(preg_match($pattern, $url, $params)) {
array_shift($params);
return call_user_func_array($callback, array_values($params));
}
}
}
}
index.php
<?php
// Create the Home URL
$url_base = trim(str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])), '/');
$url_home = 'http://' . trim($_SERVER['HTTP_HOST'], '/') . (trim($url_base) !== "" ? '/' . $url_base : "");
// Load the include class
spl_autoload_register(function($class){
include 'functions/' .$class. '.php';
});
session_start();
// Index Page => `blog`, `blog/1`
route::route_add(array('blog', 'blog/(:num)'), function($offset = 1) use($url_home) {
template::template_create('blog', array(
'title' => 'Blog Page',
'func' => new func(),
'offset' => (int) $offset
));
});
// Category Page => `category/category-slug`, `category/category-slug/1`
route::route_add(array('category/(:any)', 'category/(:any)/(:num)'), function($slug = "", $offset = 1) use($url_home) {
template::template_create('category', array(
'title' => 'Category Page',
'category' => $slug,
'offset' => (int) $offset
));
});
// User Submit
route::route_add('submit', function() use($url_home) {
if(session::exists('username')){
template::template_create('submit', array(
'title' => 'Submit Page',
'func' => new func(),
'ckeditor' => 'templates/js/ckeditor.js'
));
}
else {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
template::template_create('404', array(
'title' => '404 Not Found',
'content' => '<p>Article not found.</p>'
));
}
});
// Login Page => `login`, 'login/success'
route::route_add('process/(:any)', function($slug = "?") use($url_home) {
if(session::exists('username')){
template::template_create('process', array(
'slug' => $slug,
'func' => new func()
));
}
else {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
template::template_create('404', array(
'title' => '404 Not Found',
'content' => '<p>Article not found.</p>'
));
}
});
// Article Page => `article/article-slug`
route::route_add('article/(:any).md', function($slug = "") use($url_home) {
if(func::post($slug) == true) {
template::template_create('article', array(
'title' => 'Article Page',
'slug' => $slug,
'func' => new func(),
'content' => 'articles/post.php'
));
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
template::template_create('404', array(
'title' => '404 Not Found',
'content' => '<p>Article not found.</p>'
));
}
});
// Login Page => `login`, 'login/success'
route::route_add('login', function() use($url_home) {
template::template_create('login', array(
'title' => 'Login Page',
'func' => new func()
));
});
// User Search
route::route_add('search', function() use($url_home) {
if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['q']) && ! empty($_POST['q'])) {
header('Location: ' . $url_home . '/search/' . urlencode($_POST['q']));
exit;
} else {
header('Location: ' . $url_home);
exit;
}
});
// Search Page => `search/search-query`, `search/search-query/1`
route::route_add(array('search/(:any)', 'search/(:any)/(:num)'), function($query = "", $offset = 1) use($url_home) {
$title = 'Search Page';
$query = urldecode($query);
template::template_create('search', array(
'title' => 'Search Page',
'query' => urldecode($query)
));
});
// Static Page
route::route_add('(:any)', function($slug = "") use($url_home) {
if(file_exists('pages/' . $slug . '.md')) {
template::template_create('page', array(
'title' => 'Static Page',
'slug' => $slug,
'content' => file_get_contents('pages/' . $slug . '.md')
));
} else {
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
template::template_create('404', array(
'title' => '404 Not Found',
'content' => '<p>Page not found.</p>'
));
}
});
// Home Page => `/`
route::route_add('/', function() use($url_home) {
template::template_create('home', array(
'title' => 'Home Page'
));
});
// Do Routing
route::route_execute();
// Fallback to 404 Page if Nothing Matched
header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
template::template_create('404', array(
'title' => '404 Not Found',
'content' => '<p>Page not found.</p>'
));
article.php
<?php include 'header.php'; ?>
<?php echo '<p><strong>Article Slug:</strong> ' . $slug . '</p>'; ?>
<?php include $content; ?>
<?php include 'footer.php'; ?>
post.php
<?php
echo "<h2>".$article['nama']."</h2><br/>";
echo $article['alamat'];
?>
db.php
<?php
/*
8"""" 8""8""8
8 eeeee eeeee e e eeee e 8 8 8 e e eeeee eeeee e e e e
8eeee 8 8 8 8 8 8 8 8 8e 8 8 8 8 8 8 8 8 8 8 8 8
88 8eee8 8e 8 8eee8 8eee 8e 88 8 8 8e 8 8eee8e 8eee8 8eee8 8eeee8
88 88 8 88 8 88 8 88 88 88 8 8 88 8 88 8 88 88 8 88
88 88 8 88ee8 88 8 88ee 88eee 88 8 8 88ee8 88 8 88 88 8 88
*/
class db{
protected $_db;
private $HOST = 'localhost',
$DBNAME = 'cekula2',
$USER = 'fadhel',
$PASS = '';
public function __construct(){
try
{
$this->_db = new PDO( "mysql:host=$this->HOST;dbname=$this->DBNAME",$this->USER,$this->PASS );
$this->_db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo $e->getMessage();
echo "<br/>";
echo "GAGAL KONEK";
}
}
}
?>
[SOLVED] Ternyata harus pake static dan manggilnya pake self::
1
3 Jawaban:
kemungkinan besar akar masalah agan disini
// index php line 68
if(func::post($slug) == true) {
agan pakai scope resolution operator (::) untuk non static method. coba pakai object operator biasa aja
// index php line 68
if(func->post($slug) == true) {
0
malah undefined variablenya [img] https://preview.ibb.co/h0oGOF/Screenshot_986.png [/img]
0