Google Analytic on a php site? - php

I have some problem to get the Google Analytics to work. I have got a analyticsTracking.php file and this is included on my index.php page like this :
<?php
include("begin.php");
include($subroot . "templateconfig.php");
include($cms['tngpath'] . "genlib.php");
include($cms['tngpath'] . "getlang.php");
include($cms['tngpath'] . "$mylanguage/text.php");
tng_db_connect($database_host,$database_name,$database_username,$database_password) or exit;
include($cms['tngpath'] . "checklogin.php");
//Insert the following lines in your index.php to take advantage of template switching
if($templateswitching && $templatenum) {
include($cms['tngpath'] . "templates/template$templatenum/index.php");
exit;
}
//end of lines to be inserted for template switching
$flags['noicons'] = true;
$flags['noheader'] = true;
tng_header( $text['mnuheader'], $flags );
?>
<!-- "Home Page" (the text for these messages can be found at near the bottom of text.php -->
<h1><?php echo $text['mnuheader']; ?></h1>
<p><strong>Please customize this page!</strong></p>
<?php include ("analyticsTracking.php"); ?>
</body>
</html>
When looking on the genereted index.html there is no data from the analyticsTracking.php at all? Why?
Pleas note, I am not working with PHP so all this is new to me.
Edit1: Sorry, I am using index.php and not index.html.
Edit2: analyticsTracking.php contains somthing like this :
<script type="text/javascript">
var gaJsHost = ((" https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try{
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._trackPageview();
} catch(err) {}
</script> <?php
// End Analytics tracking code
?>

So if I'm understanding correctly, your index.php script includes analyticsTracking.php, yet when you view index.php in a browser, you don't see any of the HTML from analyticsTracking.php.
<?php include ("analyticsTracking.php"); ?>
Is that the correct path? Is analyticsTracking.php in the same directory as index.php? Try changing the include to a require and see if it prints an error when the webpage is refreshed.

Sorry, the problem was that I used the wrong php file. It is working as it should.

Related

PHP sessions working differently xampp and web

When I try to open js.php, it gives an error in xampp while in 000webhost it doesn't . I don't understand why.
so here is the code.
js.php
<?php
header('Content: text/javascript');
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
index.php
<?php
session_start();
$_SESSION['myjskey'] = hash('md5','examplekey');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP</title>
</head>
<body>
this is a test.
<script type="text/javascript"><?php include('js.php'); ?></script>
</body>
</html>
Website js.php
Xampp Js.php
Edit 1: I cannot do session_start(); in js.php as it will give an error index.php
(session already started.) Removing the header didn't work.
<?php
//js
if($_SESSION['myjskey'] != hash('md5','examplekey')){
die('No Auth');
}
$_SESSION['JSSESSID'] = 'change';//so that no one can access directly
?>
//secret js
alert('javascript done.');
//and some more js
make this change no need to use
header('Content: text/javascript');

php jquery mysql rating counter

I'm working with a CMS project.
Currently I want to build a rating system there but unfortunately that rating system require's JQUERY I'm # learning position on jQuery.
buy my knowledge in working with this rating system.
db ::tables ::columns = id,path,likes,dislikes..
Index.php
<?php
include 'db.php';
include 'conf.php';
$path = $home_path;
$q = "select * from likes where path='".$path."'";
$res = mysqli_fetch_assoc(mysqli_query($mysqli ,$q));
echo 'Likes('.$res["yes"].') ';
echo 'Unlikes('.$res["no"].')';
**Here I wanted to send my parameters with like() function and pass them via jQuery and contact the php page ..and I want the results back from that php page. Like I want to get the json reply **
Kindly please help me.
I meant I need that full jquery code.
And can you please explain me how is that working.
Like what I tried to say is:
index.php & #post_rating.php
<head>
<title>The jQuery Example</title>
<script type ="text/javascript"
src ="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
</script>
<script type ="text/javascript" language="javascript">
$(document).ready(function() {
$("#like").click(function(path,type){ //i want to get the values
with this function here
$.ajax({
type: "POST",
url: "post_rating.php",
data: "path=" +path + "&type=" +type,
success: function(){alert('success');}
/////i want to show the the
reply which is producded by php-json page post_rating.php
});
});
});
</script>
</head>
<body>
<?php
include 'db.php';
include 'conf.php';
$path = $home_path;
$q = "select * from likes where path='".$path."'";
$res = mysqli_fetch_assoc(mysqli_query($mysqli ,$q));
echo 'Likes('.$res["yes"].') ';
echo 'Unlikes('.$res["no"].') ';
?>
#post_rating.php
<?php
if(isset($_REQUEST["path"]) && isset($_REQUEST["type"])){
$path = $_REQUEST["path"];
$type = $_REQUEST["type"];
if($type =="dislike"){
$reply = 'you disliked this page '.$path.'';
}
elseif($type =="like"){
$reply = echo 'you lick this page : '.$path.'';
}
json_encode($reply);
}
?>

PHP: Show HTML Only On Homepage

I'm having trouble getting a PHP script to work. It doesn't seem to be working accurately. I'm trying to make a script to only show HTML or Javascript on the homepage only. This script will be used for multiple things. I did google this, but most answers are for WP. This is vanilla PHP.
Here's what I used:
<!-- Add Only On The Homepage Or Else -->
<?php
$currentpage = $_SERVER['REQUEST_URI'];
if($currentpage=="/" || $currentpage=="/index.php" || $currentpage=="" ) {
echo '
<header id="main-header" class="home">
';
}
else {
echo '
<header id="main-header">
';
}
?>
The results are that it shows the content of else, even though it's the homepage. I tested it on "/" and "/index.php".
I commonly use this snippet:
index.php
if (preg_match('/^m\.[^\/]+(\/(\?.*|index\.html(\?.*)?)?)?$/i', $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'])) {
readfile('index.html');
} else {
// Your framework bootstrapper or your own logic
}

Load a html file

Probably i didnt explain very well, i updated the code to turn more easy to see what i want.
I need to load a external html file in that "div external_page....", i thing that the best way is to load a file in the php file(External PHP content), tell me if im wrong :)
This is the demo: http://vasplus.info/demos/load_and_refresh_div_every_10_seconds/index.php
(UPDATE)
<script language="javascript" src="js/jquery_1.5.2.js"></script>
<div id="external_page_content_displayer">External page contents will be here</div>
<!-- EXTERNAL PHP CONTENTS-->
<?php
srand((float) microtime() * 10005224);
$lines = file('ajax/rssatom/rss-atom.html');
$This_Page_Content = array($lines,
"teste","teste","teste");
$This_Page_Content_Rand_Keys = array_rand($This_Page_Content, 2);
$This_Page_Content_Displayer = $This_Page_Content[$This_Page_Content_Rand_Keys[0]] . "\n";
echo strip_tags($This_Page_Content_Displayer);
?>
<!-- LOAD FUNCTION-->
<script>
function Load_external_content()
{
$('#external_page_content_displayer').load('external_content.php').hide().fadeIn(3000);
}
setInterval('Load_external_content()', 10000);
</script>
Regards
You can try with this:
$lines = file('ajax/rssatom/rss-atom.html');

How do I use PHP to supply info to JavaScript?

I have a simple image-looping script that changes the src of an image.
function cycleNext()
{
++imgIndex;
if(imgIndex>imgCount)
{
imgIndex = 1;
}
setImgSrc(imgIndex);
}
However, at present, I'm (shudder) manually entering imgCount in my script. The alternative is server-side, but I don't know how to fetch this information. I imagine it's pretty simple, though.
How can I use PHP to supply this script with the number of images in the folder?
<?php
$directory = "Your directory";
$filecount = count(glob("" . $directory . "*.jpg"));
$filecount += count(glob("" . $directory . "*.png"));
?>
Repeat the 2nd line for each extension you wish to count.
function cycleNext()
{
++imgIndex;
if (imgIndex > <?php echo $filecount;?>)
{
imgIndex = 1;
}
setImgSrc(imgIndex);
}
That should do it.
EDIT:
function cycleNext(imgCount)
{
++imgIndex;
if (imgIndex > imgCount)
{
imgIndex = 1;
}
setImgSrc(imgIndex);
}
Then when you call cycleNext, call it with the variable.
cycleNext(<?php echo $filecount; ?>);
if the .js file is a separate file. then you can do this:
change the .js for a .php
then you can add <?php ?> tags just like you do in your .php files.
just don't forget to add the header in the code, indicating that the file is a javascript file. like that:
<?php header("Content-type: text/javascript"); ?>
and you will call the file with it's actual name src="file.php"
You can do it in three ways:
Making your .js file a .php file (with the correct mime-type) and just use an echo in that .js.php-file
include the javascript to the <head> tag of your page
echo a variable into a <script> tag in your <head> and use it in your javascript file. Example:
<script type="text/javascript">
var imgCount = <?php echo $imagecount ?>
</script>;
During the generation of the HTML code, simply insert a <script> line, for instance
echo '<script type="text/javascript">';
echo 'var imgCount=' . $NumberOfImages . ';';
echo '</script>';
Just ensure that line is provided before cycleNext() is called (or imgCount is used).

Categories