<?php echo base_url();?> and <?php base_url();?> - php

<script src='<?php base_url();?>assets/js/jquery.min.js'></script>
the script above is code that my friend use, that works just for him, but if i change it to <?php echo base_url();?> and it's work for me, not for him. It's become a problem when we transfer files each other, What i am supposed to do ?
thank you.
I've solution script for my case, change this from config.php :
$http = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '') . '://';
$urlbaru = str_replace("index.php","", $_SERVER['SCRIPT_NAME']);
$config['base_url'] = "$http" . $_SERVER['SERVER_NAME'] . "" . $urlbaru;

Please show more about your problem detail like paste your error code in here that we could use those information to help you.
Btw, base_url() isn't a php embedded function, one of the potential problem is that your friend custom a function named base_url() that supposed to return it's root path.
So, you have to either include or build the same function with the same name to your script.
Ex.
function base_url()
{
return dirname($_SERVER['SCRIPT_FILENAME']);
}
And the 'src' inside your tag should point to your jQuery source file.

Looks like you are using codeigniter framwork, and you have not autoloaded URL helper, that's why you getting error. if I am not wrong you have to load URL helper.

Related

PHP - send result from a function to included file

I want pass result of a function to an included file in that function. let me explain it with an example .
index.php
$siteurl = $_POST['url'];
function validator($siteurl){
global $lang, $siteurl;
......
......
......
return $output;
}
en.php
$lang = array();
$lang['site_url'] = 'Site url is' .$output. '';
$lang['site_url_delete'] = 'delete' .$output. '';
$lang['site_url_edit'] = 'edit ' .$output. '';
well, now i want pass $output from validator function from index.php to fa.php ($lang).
I put this code in fa.php:
$siteurl = $_POST['url'];
$output = validator($siteurl);
It works but it's dirty work because I don't want to call a function (Validator) to each $lang. is there better way to do this?
All files you include in php share the enclosing scope. Think about this like you copy all code from the included file and insert it to the "main" file instead of the include statement.
So, you can write $output = validator($siteurl) in your index.php file, include your lang.php file and directly use the $output variable.
But, however, it is kinda bad practice to use global variables. It may turn your code to a mess after some time. So, be careful and consider to rethink an architecture of your application.
I've been in a wrong way! found the best solution!
solved my problem with function.sprintf
sprintf($lang['site_url'], $output)
$lang['site_url'] = 'Site url is %s';

How to insert an image using a class (dirname and filepath issue)

I have a method in a class I would like to use in order to show an image in a page that could be placed in any directory.
Basically, the class file would be at the root directory and the class would look like this:
class A{
function showImage(){
echo '<img src="img/file.jpg">';
}
}
Problems occur when I call the method in a page which is in a subdirectory.
For example, if I use showImage() in 'subdir/mypage.php', then the image link is broken.
I have looked into $_SERVER['HTTP_HOST'] and dirname(__FILE__) but I coud not find the right mix to get the right image path that could be used in order to show the image from any directory.
What function should I used in the class to get the file path dynamically that would depending on the directory where I am calling showImage()?
Many thanks
class A{
function showImage($link){
$link = str_replace('/home/public', '', $link);
echo '<img src="' . $link . '">';
}
}
and use it like this:
$a->showImage(getcwd().'/img/file.jpg');
The getcwd() will get the directory, then remove the first absolute path to your public folder in the function.
Example case:
test.php is placed in /home/public/images/{test.php}
class.php is placed in /home/public/{class.php}
test.php does:
$a->showImage(getcwd().'/img/file.jpg');
What it'll send to class.php is:
$a->showImage('/home/public/images/img/file.jpg');
Then in your function it'll remove /home/public and show this link:
<img src="/images/img/file.jpg">
MY BAD
I didn't read the answer right. Here's the solution you're looking for:
class A{
function showImage(){
$root = ($_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . '/';
echo '<img src="' . $root . 'img/file.jpg">';
}
}

APPLICATION PATH in Zend Framework getting appended to virtual host name

In my Layout, when I do this
<?php $js = APPLICATION_PATH."/../public/js"; ?>
<script src = <?php echo $js."/signup_layout_js.js"; ?> ></script>
I get a js error
http://proj_name/home/aman/Work/proj_name/public/js/signup_layout_js.js
My APPLICTION_PATH is getting appended to http://proj_name/, I dont want that.
But if I do this, it works fine on http://proj_name/ but breaks when i go to a controller/action like http://proj_name/Controller/Action
<script src ="./js/signup_layout_js.js"></script>
How can I correct this. Thanks.
Also in my index.php, APPLICATION_PATH is
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
Thanks..
Link scripts like this:
<script src = "<?= $this->baseUrl() . '/js/signup_layout_js.js' ?>" ></script>
Or use headScript helper:
//put this in header
<?= $this->headScript(); ?>
//put this before closing body tag
<?= $this->inlineScript() ?>
//and this anywhere in you code ($this is view object)
$this->headScript()
->prependFile($this->baseUrl() . '/js/jquery-1.7.2.min.js')
->appendFile($this->baseUrl() . '/js/flying-header.js')
->appendScript('
var google_conversion_id = 9999999;
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;');
Your entire path is part of the src for your scripts because you are prepending it. Try echoing the value of your APPLICATION_PATH constant. This is not the correct behaviour for referencing static files in web applications.
In your case, you should look into using Zend Framework's built-in headScript and inlineScript view helpers for this. You shouldn't need to write the script DOM tags yourself.

How to communicate view and controller in a php project with out any type of framework

Backgroud / Idea
I am creating a web aplication from scratch; with out any type of php framework.
I am creating the web application in this way; because i want to LEARN php; and i think
that use a php framework do not will mame me a good php developer.
What i did so far?
I create a project structure:
Project
css
script
Models
Controllers
Views
Resource
I created one view like:
<html>
<head>
<!-- here i call my jquery.js -->
<script>
...
function action()
(
...
$.ajax(url: "/Controllers/HomeController.php", type:"POST",DataType:"JSON",Parameter:{var1:$("#var1").val(),method:"singup"})
{...}
...
)
...
</script>
</head>
<body>
<input id="var1" name="var1" type="text">
<input id"mybutton" name="mybutton" type="button" onclick="action();">
</body>
</html>
In my HomeController.php
<?php
if(if(is_set($POST('method'))))
{
if(function_exist($POST('method')))
{
switch($POST('method'))
{
case "singup":
$var1 = $POST('var1')
singup($var1);
break;
...
default: break;
}
}
}
function singup($var1)
{
$array = {"var1" => $var1};
json_decode($array,1);
}
?>
Questions
I am not really sure about this code (security). I want to know:
What is the recommended way make call between views and controllers with out
use any type of php framework ?
I myself like to have all this events and ajax calls within JS files, and as you are using a MVC like structure try having having a logic that handle URI routes like.
/controller/method/param1/param2
that way you would have a better code structure your above code then would refer to something like.
/home/signup/param1......
For this you would have to first get the URL divide it into segments for example.
$controller = $segment[0]
$method = $segment[1]
$params = ... the rest of the segments
here then you could have a check to see if the controller and method exists, and if the method has the given number of parameters. Try having a look at the laravel framework source code as they great work, I bet there's a couple of thinks you can pick out of it.
Well, I'm not sure if i read your question correctly but...
in your controller you do all the PHP stuff then when you want to load your view you can use the
include command to load your html.php page. This way your PHP variables are exposed to your view.
But if your learning, it doesn't hurt to crack open an existing framework and look around to see what approach they took.
Also, in my framework I create a folder called "assets" and i include all the css, img, and js directories in it.
As for any framework, I'll share with your the snippet i use to get the URI and pull out the controller,action, and params
self::$_params = explode('/', rawurldecode(trim(parse_url(getenv('REQUEST_URI'), PHP_URL_PATH), '/')));
$scope = array_shift(self::$_params);
$pre = null;
$pre = strlen(self::$_controller) > 1 && is_numeric(self::$_controller[0]) ? 'n' : null;
$pre = strlen(self::$_controller) > 1 && self::$_controller[0] == '-' ? 'd' : $pre;
self::$_controller = $pre . preg_replace('/-/', '_', self::$_controller) . '_Controller';
self::$_callAction = array_shift(self::$_params);
if (empty(self::$_callAction) || self::$_callAction == '-')
{
self::$_callAction = 'indexAction';
} else {
$pre = strlen(self::$_callAction) > 1 && is_numeric(self::$_callAction[0]) ? 'n' : $pre;
$pre = strlen(self::$_callAction) > 1 && self::$_callAction[0] == '_' ? 'u' : $pre;
$pre = strlen(self::$_callAction) > 1 && self::$_callAction[0] == '-' ? 'd' : $pre;
self::$_callAction = preg_replace('/-/', '_', self::$_callAction);
self::$_callAction = $pre . self::$_callAction . 'Action';
}

How to get the current url in a OpenCart .tpl file?

I'd like to get the current url in a .tpl file in OpenCart.
I've seen this answer but it works only in a php file.
I have to get this way:
_my_array.push(['_productName',"<?php echo $heading_title; ?>"]);
**_my_array.push(['_productUrl', ["how can I get url ?"]]);**
Thanks
To get full url
<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
and btw those answers work on tpl files also because tpl files are sort of php files only
The best practice would be to get it in the controller then use it in the view file.
In your controller
$data['current'] = $this->url->link($this->request->get['route'], '', 'SSL');
Then in the view file
echo $current;
I also needed current url for schema.org.
Generally you can create current url via link function
public function link($route, $args = '', $secure = false)
So https link for product page that would be
$data['share'] = $this->url->link('product/product', 'product_id=' . (int)$this->request->get['product_id'], true);
Then in the view file
echo $share

Categories