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
Related
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"; ?>
I am a newbie in programming and now studying about encryption on password storage, and during my experiments i ran into this login problem. I am trying to login comparing the hashed password in database and user input using php crypt() function:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<form method="post" action="index.php">
user name :<input type="text" name="uid" />
password :<input type="text" name="pas" />
<input type="submit"/>
</form>
<?php
if(isset($_POST['pas'])&&isset($_POST['uid']))
{
$uid=$_POST['uid'];
$pas=$_POST['pas'];
require_once('class.DBConnect.php');
$ob=new DBConnect('test');
$ob->getData("select * from log where uid='$uid'",array('pas'));
foreach($ob->columnData as $value)
{
$hashed_pas=$value;
}
if(crypt($pas,$hashed_pas)==$hashed_pas)
{
echo "loggin in";
}
else
{
echo "fail!!!";
}
}
?>
</body>
</html>
The password is 'san'.
The value of $hashed_pas is : $1$mG5.1k/.$/.
and of crypt($pas,$hashed_pas) is: $1$mG5.1k/.$/.LHc4JCN6GRznyYWZ/Mi.
I want to know why is this happening.
I have used the auto-generated salt for hash stored in database.
class.DBConnect.php :
<?php
class DBConnect
{
public $columnData=array();
private $con,$rs;
public function __construct($database)
{
$this->con=mysqli_connect("localhost","root","root",$database);
if(mysqli_connect_errno())
{
echo "DB error is:".mysqli_connect_error();
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function make_safe($variable)
{
$variable = mysqli_real_escape_string($this->con,trim($variable));
return $variable;
}
function getData($query,$column=array())
{
$this->rs=mysqli_query($this->con,$query);
while($row=mysqli_fetch_array($this->rs))
{
foreach($column as $a)
{
$this->columnData=array($row[$a]);
}
}
}
function checkDataExist()
{
if(mysqli_num_rows($this->rs)==0)
{
return 0;
}
else
{
return 1;
}
}
}
?>
Please help me with this issue and suggest me industry level standards on these.
edit: I tried following codes and output was:
san : $1$pb2.8C3.$WhJ/zOEWZUXc/7fTEbcJe.
san : $1$pb2.8C3.$WhJ/zOEWZUXc/7fTEbcJe.
$pas="san";
$hash= crypt($pas);
echo "$pas : $hash<br />";
$hash= crypt($pas,$hash);
echo "$pas : $hash";
so I am wondering why database comparison not working.
It seems like the length of your database field is not sufficient to hold the entire hashed string. If the string to be stored exceeds the length of the table field the data will get truncated.
I have an object in PHP with 5 attributes using the following code:
<?php
class Person
{
private $gender, $race, $height, $weight, $eyes_color;
public function start ($gender,$race,$height, $weight, $eyes_color)
{
$this->gender=$gender;
$this->race=$race;
$this->height=$height;
$this->weight=$weight;
$this->eyes_color=$eyes_color;
}
public function show_attributes()
{
return sprintf("%s, %s, %s, %s, %s", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
}
}
$person=new person();
?>
I'm calling this class using the following HTML code
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class Person</title>
</head>
<body>
<?php
require_once("Person.php");
$person->start("Male","Latin","1.83 cm","85 kg","Brown");
echo $person->show_attributes();
?>
</body>
</html>
Now, that will print something like
Male, Latin, 1.83 cm, 85 kg, Brown
But I want to print something like
--------------------------------------
|Male | Latin | 1.83 cm | 85 kg | Brown|
--------------------------------------
Using a HTML table.
I have try a couple of things, but I can't make it happend.
Is there a way to force
echo $person->show_attributes();
to only show one attribute so I can call it from inside a HTML cell table?
Thanks.
Try this
<?php
class Person
{
private $gender, $race, $height, $weight, $eyes_color;
public function start ($gender,$race,$height, $weight, $eyes_color)
{
$this->gender=$gender;
$this->race=$race;
$this->height=$height;
$this->weight=$weight;
$this->eyes_color=$eyes_color;
}
public function show_attributes()
{
return sprintf("<td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td>", $this->gender, $this->race, $this->height, $this->weight,$this->eyes_color);
}
}
$person=new person();
?>
HTML code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Class Person</title>
</head>
<body>
<?php
require_once("Person.php");
$person->start("Male","Latin","1.83 cm","85 kg","Brown");
echo "<table>":
echo "<tr>";
echo $person->show_attributes();
echo "</tr>";
echo "</table>";
?>
</body>
</html>
I don't know how in-depth you're looking to go, but you can load the data into a data modeler/table system like http://backgridjs.com.
I realize that may be completely over the top for what you're looking for, but it's robust and (mostly) easy to learn.
I'm new to the Kohana Framework. I have a problem - How can I pass the variable $title from Layout.php to Head.php?
In controller:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Admin_Quanly extends Controller_Template {
public $template='admin/layout';
function _showWithTemplate($subview,$title)
{
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->title = $title;
}
public function action_index()
{
$this->_showWithTemplate('subview/home','Trang quản trị hệ thống');
}
}
In view Layout.php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php echo $head?>
</head>
<body>
</body>
</html>
In view Head.php:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?=$title?></title>
<base href="<?=URL::base()?>">
<link rel="stylesheet" type="text/css" href="style.css" />
<script type="text/javascript" src="javascript/jquery.min.js"></script>
<script type="text/javascript" src="javascript/ddaccordion.js"></script>
You could do something like this:
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->head->title = $title;
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->title = $title;
Note the $this->template->head->title = $title; you need to pass it along manually to the head view.
You can use set() or bind(). See example:
$view = View::factory('user/roadtrip')
->set('places', array('Rome', 'Paris', 'London', 'New York', 'Tokyo'));
->bind('user', $this->user);
Ref: http://kohanaframework.org/3.3/guide/kohana/mvc/views
What you are looking for is set_global
http://docs.kohanaphp.com/core/view#set_global
It will allow you to set a variable for all your views to be able to use. You won't be passing it per say but it will still do what you want.
Example fix
function _showWithTemplate($subview,$title)
{
$admin_path = 'admin/';
$this->template->head = View::Factory(''.$admin_path.'head');
$this->template->subview = View::Factory(''.$admin_path.''.$subview.'');
$this->template->set_global('title', $title);
}
So, I have a template engine I made and I want to replace {pageBody} with contents from a PHP file, when I do it using the template engine it does not execute the PHP, rather it displays it in view source option.
TEMPLATE.PHP
<?php
class TemplateLibrary {
public $output;
public $file;
public $values = array();
public function __construct($file) {
$this->file = $file;
$this->file .= '.tpl';
$this->output = file_get_contents('templates/'.$this->file);
}
public function replace($key, $value)
{
$this->values[$key] = $value;
}
public function output() {
foreach($this->values as $key => $value)
{
$ttr = "{$key}";
$this->output = str_replace($ttr, $value, $this->output);
}
return $this->output;
}
}
index.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{page_title}</title>
{page_style}
</head>
<body>
{page_header}
{page_body}
{page_footer}
</body>
</html>
HTML replace works fine, but PHP does not. Any ideas?
You are rather scattered with your files and the list is incomplete. I've listed all the files you've provided so far below.
#Jonathon above is correct, that you will need to use output buffering to capture the output of the PHP file and include() the file (so it gets executed) instead of using file_get_contents() (which does not execute the file).
[edit] I re-created all these files in my local environment and confirmed that #Jonathon's suggestion worked perfectly. I've updated dev/replacements.php to include the suggested code.
Additionally, I added two more functions to your TemplateLibrary class : replaceFile($key, $filename) that does the file_get_contents($filename) so that you don't have to repeat it so often, and replacePhp($key, $filename) that performs an include() while capturing the output, so you can encapsulate the complexities of including a PHP file.
Good Luck!
main.php
<?php
require_once 'dev/dev.class.php';
require_once 'dev/templatelibrary.php';
$dev = new dev('netnoobz-billing');
$dev->loadLib('JS', 'js', 'jquery');
// template library and required files
$template = new TemplateLibrary('index');
require_once 'dev/replacements.php';
echo $template->output();
dev/templatelibrary.php
<?php
class TemplateLibrary {
public $output;
public $file;
public $values = array();
public function __construct($file) {
$this->file = $file;
$this->file .= '.tpl';
$this->output = file_get_contents('templates/'.$this->file);
}
public function replace($key, $value)
{
$this->values[$key] = $value;
}
public function replaceFile($key, $filename)
{
$this->values[$key] = file_get_contents($filename);
}
public function replacePhp($key, $filename)
{
ob_start();
include($filename);
$data = ob_get_clean();
$this->values[$key] = $data;
}
public function output() {
foreach($this->values as $key => $value)
{
$ttr = "{$key}";
$this->output = str_replace($ttr, $value, $this->output);
}
return $this->output;
}
}
index.tpl
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>{page_title}</title>
{page_style}
</head>
<body>
{page_header}
{page_body}
{page_footer}
</body>
</html>
replacements.php
<?php
$configStyleSheet = '<style type="text/css">'
. file_get_contents('styles/default/main.css')
. '</style>';
$pageHeader = file_get_contents('templates/header.tpl');
$pageFooter = file_get_contents('templates/footer.tpl');
#$pageBody = file_get_contents('loaders/pageBody.php');
ob_start();
include('loaders/pageBody.php');
$pageBody = ob_get_clean();
$template->replace('{page_style}' , $configStyleSheet);
$template->replace('{page_title}' , 'NetBilling');
$template->replace('{page_header}', $pageHeader);
$template->replace('{page_footer}', $pageFooter);
$template->replace('{page_body}' , $pageBody);
loaders/pageBody.php
<?php echo 'test'; ?>
[edit] added loaders/pageBody.php from OP's comment.
[edit] Updated dev/replacements.php to capture output buffer and use include on .php
You're using file_get_contents in your pastebin code but you should be using your template processor or PHP's include() instead. If you do $template->replace('{page_header}', $pageHeader), $pageHeader is just the source of the tpl and your template processor does not know that so it will just replace the tag with that source. Fix:
$pageHeader = new TemplateLibrary('header');
// ...
$template->replace('{page_header}', $pageHeader->output());
For the PHP files, you should call include() on the file, wrapped in output buffering, so you can pass the output of the PHP execution as the template variable, instead of the PHP source itself:
ob_start();
include('loaders/pageBody.php');
$pageBody = ob_get_clean();
/// ...
$template->replace('{page_body}', $pageBody);