PHP Custom Class error [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
Hello for an easy data handling I though of creating a class but I think I must have missed something as it stops the page from loading when I include it.
I think that the error must be something very simple but I don't have much experience in coding in php so if someone could point me in the direction of the mistake and give me an idea of what to implement to resolve this issue.
Like said above in this scenario it will ouput the page until test and then it wont put the rest.
index.php:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
</head>
<body>
<p>test</p>
<?php include("class_lib.php"); ?>
<p>test2</p>
</body>
</html>
class_lib.php:
<?php
class MyAbility{
var $Name;
var $URL;
var $Level1;
var $Level2;
var $Level2;
var $Level4;
var $Level5;
function set_Name($new_name) {
$this->Name = $new_name;
}
function get_Name() {
return $this->Name;
}
function set_URL($new_URL) {
$this->URL = $new_URL;
}
function get_URL() {
return $this->URL;
}
function set_Level1($new_Level1) {
$this->Level1 = $new_Level1;
}
function get_Level1() {
return $this->Level1;
}
function set_Level2($new_Level2) {
$this->Level2 = $new_Level2;
}
function get_Level2() {
return $this->Level2;
}
function set_Level3($new_Level3) {
$this->Level3 = $new_Level3;
}
function get_Level3() {
return $this->Level3;
}
function set_Level4($new_Level4) {
$this->Level4 = $new_Level4;
}
function get_Level4() {
return $this->Level4;
}
function set_Level5($new_Level5) {
$this->Level5 = $new_Level5;
}
function get_Level5() {
return $this->Level5;
}
}
?>

Declared twice my friend :-)
var $Level2;
https://phpcodechecker.com/
More about forcing to print out the error.
http://www.ronaldpringadi.com/archives/introduction-to-logging-and-tracing-in-php/

Related

A random number (4) just appears in my php project

Im building a php project.
This is the structure
This is my init.php
<?php
use App\Core\Container;
require_once __DIR__."/../autoloader.php";
require_once __DIR__."/Core/Container.php";
$container = new Container();
which gets used within the index.php
<?php
require "./src/init.php";
$pathinfo = $_SERVER["PATH_INFO"];
$routes= [
"/team" => ['TeamController',
'showTeampage'],
];
if(isset($routes[$pathinfo])){
$controllername = $routes[$pathinfo][0];
$method = $routes[$pathinfo][1];
$controller = $container->make($controllername);
}
This is the container php where the problem occurs:
<?php
namespace App\Core;
use PDO;
use App\Team\TeamController;
use App\Team\TeamRepository;
class Container {
public $storage = [];
public $buildManuals = [];
public function __construct() {
$this->buildManuals = [
'pdo'=> function () {
$pdo = new PDO('mysql:host=localhost;dbname=vanillaPHP;charset=utf8', 'root', '');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
return $pdo;
},
'TeamController'=> function() {
$controller = new TeamController();
},
'TeamRepository'=>function() {
return new TeamRepository($this->make("pdo"));
},
];
}
public function make(String $string) {
if(empty($this->storage[$string]) ) {
$this->storage[$string] = $this->buildManuals[$string]();
}
return $this->storage[$string];
}
When Im entering the following URI he makes a TeamController onject I have tested it.
But he prints out the number 4 in UI for what ever reason. Why is this happening?
This is my TeamController class btw
namespace App\Team;
use App\Core\AbstractController;
use App\Team\TeamRepository;
class TeamController {
public $name ="test";
// public function __construct(Teamrepository $teamRepo)
// {
// $this->teamRepo = $teamRepo;
// }
public function hi() {
echo "hi";
}
// public function showTeampage() {
// $teammembers = $this->teamRepo->fetchAll();
// $this->render($this->teamRepo->getTableName(),
// [
// 'params'=>$teammembers
// ]);
// }
}
This is the viewfield for the team:
<?php require __DIR__."../../../Components/Head.php"; ?>
<p>Hi im the team view</p>
<!-- TODO: display the team data in a beautiful way -->
<?php foreach($params as $teammember): ?>
<p>
<!-- <?php echo $teammember->firstname ?> -->
</p>
<?php
endforeach;
?>
<?php require __DIR__."../../../Components/Footer.php"; ?>
And this is the Components/Head.php:
<?php
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<title>The company</title>
</head>
<body>

Fetch a variable defined in a different function inside a shortcode function

I'm trying to fetch a variable which is defined in a different function inside the same file into a different shortcode function, but it's not reflecting it, can anybody please let me know what I'm doing wrong
if ($hits[0]<= $varlinkhits)
{
$buttonlink="$varlinkone";
}
else
{
$buttonlink="$varlinktwo";
}
?>
<?php
//creating short code
?>
<button onclick="onclickRedirect()">redirect</button>
<script>
function onclickRedirect(){
window.location.href = "<?php echo $buttonlink ?>";
}
</script>
<?php
}
session_start();
function link_button_function() {
global $buttonlink;
$_SESSION["sessionlink"] = $buttonlink;
return 'Join Whatsapp Group';
}
add_shortcode('button_link', 'link_button_function');

In my ecommerce website purchase confimation page show a warning message [duplicate]

This question already has answers here:
PHP sessions that have already been started [duplicate]
(11 answers)
Closed 5 years ago.
I am trying to developing a ecommerce website. But My problem is in product purchase confirmation page.It show this type of message: (Notice: A session had already been started - ignoring session_start() in C:\xampp\htdocs\online shop\dataAccessLayer\dalSession.php on line 6) I need to some suggestion how can I solve this issue.
Here is my code.
This is my dalSession.php page:
<?php
class Session
{
public static function Start(){
session_start(); //line 6
}
public static function Set($key , $value){
$_SESSION[$key] = $value;
}
public static function Get($key){
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
else{
return false;
}
}
public static function Stop(){
session_destroy();
header("Location:user/login.php");
}
public static function StopA(){
session_destroy();
header("Location:../user/login.php");
}
public static function StopB(){
session_destroy();
header("Location:login.php");
}
public static function Check()
{
self::Start();
//echo $this->user_id;;
if (self::Get("Mlogin")==false)
{
self::Stop();
header("Location:login.php");
}
}
public static function CheckA(){
self::Start();
if (self::Get("Alogin")==false) {
self::StopA();
header("Location:../user/login.php");
}
}
public static function CheckAll()
{
if (self::Get("Mlogin")==false && self::Get("Alogin")==false)
{
return false;
}
else
{
return true;
}
}
public static function CheckUserLogin()
{self::Start();
if (self::Get("Mlogin")==false)
{
return false;
}
else
{
return true;
}
}
public static function Auto(){
self::Start();
if(self::Get("Mlogin")==false){
echo "<a style=\"color:white;\" href='user/login.php'>Login</a>";
} else {
echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
if(isset($_GET['action']) && ($_GET['action']== "logout")){
self::Stop();
}
}
}
public static function AutoA(){
self::Start();
if(self::Get("Mlogin")==false){
echo "<a style=\"color:white;\" href='login.php'>Login</a>";
} else {
echo "<a style=\"color:red;\" href='?action=logout'>Logout</a>";
if(isset($_GET['action']) && ($_GET['action']== "logout")){
self::StopB();
}
}
}
}
?>
This is My confirm.php code:
<?php
require_once("/../dataAccessLayer/dalSession.php");
require_once("/../dataAccessLayer/dalLogin.php");
session::check();
?>
<?php
if(isset($_GET['action']) && ($_GET['action']== "logout")){
Session::Stop();
}
?>
<!doctype html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Success Notification Boxes</title>
<link rel="stylesheet" type="text/css" media="all" href="style.css">
<script type="text/javascript" src="https://ajax.googleapis.com /ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div id="w">
<div id="content">
<!-- Icons source http://dribbble.com/shots/913555-Flat-Web-Elements -->
<div class="notify successbox">
<h1>Success!</h1>
<span class="alerticon"><img src="" alt="checkmark" /></span>
<p>Thanks For Your Purchase.</p>
<p>Your Purchase Will Deliver Within 72 Hours.</p>
<p>Any Question? Please Call: <b>+8801721557233</b></p>
<p></p>
</div>
<h1><a class="navbar-brand" href="../index.php">Home</a></h1>
</div><!-- #end #content -->
</div><!-- #end #w -->
</body>
</html>
You can modify your function Start. Replace:
public static function Start(){
session_start();
}
By:
public static function Start(){
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
}
It will fix your problem (the notice will not be displayed anymore). The idea is: if your session is already started, don't start it again.
Then if it does not work, remove ALL the space in your confirm.php file before the first <?php (it seems you have two spaces before the first <?php but I'm not sure it's because you copy/pasted it).
Remove spaces before
That characters are starting a session. and may be its better to use some template engine to generate html? Smarty, twig or simple php templates.

Load html/php template function

I just go to the point.
Nvm need to add more text to much code..
Trying to load a Template with php inside it but php prints in html instead.
Init.php
class Init {
public static $ROOT = '';
public static $TEMPLATE = '';
public static $SERVICE = '';
public static function start() {
// Init Paths
Init::$ROOT = str_replace("\\", "/", __DIR__);
Init::$TEMPLATE = Init::$ROOT . "/Template/";
Init::$SERVICE = Init::$ROOT . "/Service/";
// Init Template.php class
require_once(Init::$SERVICE . "Template.php");
// Load template Top.php
$top = new Template(Init::$TEMPLATE . "Layout/Top.php");
echo $top->load(); // Show Top.php
}
}
Top.php
<!DOCTYPE html>
<html>
<?
// Load template Head.php
$head = new Template(Init::$TEMPLATE . "Layout/Head.php");
$head->set("TITLE", "Dashboard"); //Set [#TITLE] to Dashboard
$head->load(); // Show Head.php
?>
</html>
Head.php
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>[#TITLE] | cwEye</title> <!-- [#TITLE] will be Dashboard-->
<?
echo "Hello"; // ERROR -> This will print <? echo"Hello"; ?> in my page
?>
</head>
Template.php
<?
class Template {
protected $file;
protected $values = array();
private static $templateFile = null;
public function __construct($file) {
$this->file = $file;
}
public function set($key, $value) {
$this->values[$key] = $value;
}
// This code works but it will not load php inside
public function load() {
if (!file_exists($this->file)) return "Error loading template file ($this->file).";
ob_start();
include_once($this->file);
$data = ob_get_clean();
foreach ($this->values as $key => $value) {
echo str_replace("[#$key]", $value, $data);
}
if(count($this->values) == 0) echo $data;
}
}
?>
Ive played with allot of functions to make it but it does not work...
It just prints the php in html.
Tried with
ob_start();
include_once(FILE);
$data = ob_get_clean();
Don't use short tags like <? or <?=, use <?php instead. You probably have your short_open_tag set to false in php.ini. If you are using PHP 7 then you should know short tags were removed completely and wont work anymore.
In head.php use the full tag. Change
to
<?php echo "hello"; ?>

sometime SetCookie() not working

Hi I created two file to switch my forum (Language Chinese and English)
enForum.php
<?php
function foo() {
global $_COOKIES;
setcookie('ForumLangCookie', 'en', time()+3600, '/', '.mysite.com');
echo 'running<br>';
$_COOKIES['ForumLangCookie'] = 'en';
bar();
} // foo()
function bar() {
global $_COOKIES;
if (empty($_COOKIES['ForumLangCookie'])) {
die('cookie_name is empty');
}
echo 'Language =' . $_COOKIES['ForumLangCookie'];
echo "<br>";
} // bar()
foo();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forum EN Version</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
please be patient ...
<script LANGUAGE='javascript'>
location.href='http://www.mysite.com/forum/index.php';
</script>
</body>
</html>
cnForum.php
<?php
function foo() {
global $_COOKIES;
setcookie('ForumLangCookie', 'cn', time()+3600, '/', '.mysite.com');
echo 'running<br>';
$_COOKIES['ForumLangCookie'] = 'cn';
bar();
} // foo()
function bar() {
global $_COOKIES;
if (empty($_COOKIES['ForumLangCookie'])) {
die('cookie_name is empty');
}
echo 'Language =' . $_COOKIES['ForumLangCookie'];
echo "<br>";
} // bar()
foo();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forum CN Version</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
please be patient ...
<script LANGUAGE='javascript'>
location.href='http://www.mysite.com/forum/index.php';
</script>
</body>
</html>
There are some files including include template('logon');,include template('regist'); etc, I write some code to get the Cookie value and control the flow to load different template files.
$lang = $_COOKIE["ForumLangCookie"];
// for Debug
// echo '$lang is '.$lang;
// echo '<br/>';
if ($lang == "cn"){
include template('logon');
}
else if ($lang == "en"){
include en_template('logon');
}
But sometime the SetCookie() not working. Do I need add Sleep(someSeconds); for my code?
Cookies can be accessed with $_COOKIE,not $_COOKIES.
EDIT:Sorry for misunderstanding. I suggest you to change the variable $_COOKIES as another common one so people can understand your question correctly.
PHP array name is $_COOKIE, not $_COOKIES

Categories