I have two scripts but I can't make them work together.
1- A simply page views counter
<?php
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Pageviews=". $_SESSION['views'];
?>
2 - A Random link from a list but without repeat the links
<?php
if (empty($_SESSION['links'])) {
// first time visit, populate random links in session
$links = array('http://some-site.com', 'http://some-other-site.com', 'http://example.com');
shuffle($links);
$_SESSION['links'] = $links;
}
$link = array_shift($_SESSION['links']);
$_SESSION['links'][] = $link;
?>
For some reason if I use one of them the other will stop to work, both had worked fine but I can't make them work together on the same site.
On the header I have <?php session_start(); ?> but I also moved the script to different parts of the site and I get always the same problem, one stop to work. I also had the <?php session_start();?> at the start of each piece of code but nothing seems to work.
At some point I manage to make both scripts work but the page views counter script was counting from 3 to 3, not from 1 to 1 - Note that the random link script have also 3 values on it; so my guess is that something is incompatible with both scripts
Any help and guide in how or where I need to place the code will be appreciated.
Thanks and sorry for my English
Daniel
try is on the top of the code
just add "$_SESSION['views'] = 0;" to the top once when u run the main script i think it will work
$_SESSION['views'] = 0;
if (empty($_SESSION['links'])) {
// first time visit, populate random links in session
$links = array('http://some-site.com', 'http://some-other-site.com',
'http://example.com');
shuffle($links);
$_SESSION['links'] = $links;
}
$link = array_shift($_SESSION['links']);
$_SESSION['links'][] = $link;
echo "<pre>";
print_r($_SESSION['links']);
echo "</pre>"
if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Pageviews=". $_SESSION['views'];
Related
Currently when i showing my username(through session) and logout function , both of them is in different lines
Image: http://prntscr.com/nc2v1b ( This is output )
What i want : https://prnt.sc/nc2w23
Username is on the right side of "Logout"
First of all, remove this part style='float:right;'
Using float:right will move the text right.
Second, need some spacing here:
echo $_SESSION["username"]." "; // you can use `|` to separation
echo '<span>Logout</span></li>';
Float right will push everything to the right (last item being the one the the most on the right).
Your code seem to work if you need to have everything on one line though.
<?php
session_start(); // Right at the top of your script
?>
<li class='active'>
<?php
if($_SESSION['logged']==true)
{
echo '<span>Logout</span>' . $_SESSION["username"];
}
else
{
echo '<span>Login/Register</span>';
}
echo '</li>';
?>
I'm troubled with some PHP session issues.
I have a main page, where i start the session, and some links to a second page. I want to keep track of the pages (breadcrumbs), so i store these in the session.
When i test, this works for some, and doesn't for others... i don't see the problem.
Page 1: (index.php)
<?php
session_start();
if (!isset($_SESSION['kruimels'])){
$_SESSION['kruimels']["home"]="index.php");
}else{
unset($_SESSION['kruimels']);
array_push($_SESSION['kruimels']["home"]="index.php");
}
?>
<a href='sub.php?id=1'>item1</a>
<a href='sub.php?id=2'>item2</a>
<a href='sub.php?id=3'>item3</a>
<a href='sub.php?id=4'>item4</a>
Second page: (sub.php)
<?php
session_start();
/* I get the name of the page by querying the DB...*/
if (isset($_SESSION['kruimels'])){
$_SESSION['kruimels'][$nameOfPage]="sub.php?id=".$_GET['id'];
}
?>
<?php
foreach ($_SESSION['kruimels'] as $naam => $path) {
echo "<li><a href='$path'>$naam</a></li>";
}
?>
The strange thing is, somethimes it gets saved, sometimes it doesn't...
i don't see the problem...
Help?
Greetings,
Martijn
you have no need to start a session for this just use basename($_SERVER['REQUEST_URI']); this function of php. This will give you the name of your current file.
After this, you got your file name and now explode this with (.) And echo wherever you want to show
And than Try below code
$pagename = explode('.', basename($_SERVER['SCRIPT_NAME']));
echo $pagename[0];
I'm trying to echo out dynamic php titles depends on page for seo purposes.
I successfully did this the pages I call from database depends on their id's.
Like that:
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
$title = $r["title"];
}
}
And this is how I echo out:
<title><?php if (isset($_GET["category_id"])) { echo $title; echo " |"; } ?> mypage.com</title>
result:
on category.php?category_id=1 Page title is: "Category 1 | mypage.com"
*
But there are pages which is not static.
for example: index.php, login.php.
*
I want to figure out how to edit my code below to print "Login" on login.php between title tags.
<title>
<?php
if (isset($_GET["category_id"])) {
echo $title; echo " |";
}
?> mypage.com
</title>
EDIT
my login.php is like that:
include("header.php");
content.
So I need to define $title for login.php in header.php
I need to add some codes to header.php when user will see different title on login.php, index.php etc.
I'm able to do it category.php?category?id=1 already with the code above, but I need to also make it for login.php, index.php and so on.
There are several ways to do this within the code you outlined.
First, I'm going to simplify some of your code a bit. This also potentially makes it slightly faster:
echo "<title>$title | mypage.com</title>";
This assumes that $title is going to be set, either by the query from when $_GET['category_id'] is set, or from the file that calls it. The great thing about includes is that they can pass variables. So in the login.php and any other file where you are not doing a GET, just specify $title in that file.
Login.php:
$title = 'Login';
include("header.php");
content.
Which would display page title of "Login | mypage.com".
I'm trying to seperate each output individually. Is there any possible way to do this ?
<?php echo stripslashes($row['meta_keys'] = str_replace(',',' ',$row['meta_keys'])) ?>
I want to link each one to a specific thing, but it all comes out as 1 a link
I did <?php echo stripslashes($row['meta_keys'] = str_replace(',',' ',$row['meta_keys'])) ?> but again, it all came out as one link. Any simple way to do this?
I am a total PHP novice and am trying to write what I think is a pretty simple script. This is the code I have so far:
HTML / PHP
<?php
$hits = file_get_contents('hits.txt');
++$hits;
file_put_contents('hits.txt', $hits);
echo $hits;
$url = $_GET['w'];
?>
<iframe src="<?php echo $url; ?>"></iframe>
<p>
<?php echo $hits; ?>
</p>
The result is a page with an iframe and a hit counter.
The problem with this script is that if the variable $url changes, the hit counter does not. My goal would be that if I visited http://www.website.com/index.php?w=blue.html I would get a different counter than if I visited http://www.website.com/index.php?w=yellow.html.
EDIT: I should add that this script is designed to accept any URL. I realize this complicates things significantly. My ultimate goal would be that if the counter didn't already exist for that particular URL, it would be generated on the fly.
Your current code saves the hit points for every page to the same file as a simple string.
You have a number of options. Here's one that would work if you prefer to stick with text files instead of databases.
You could take the URL in, hash it, and save the counter for that page in a hash-named text file.
Something like
if( isset($_GET) && !empty($_GET['W']) ){
$url = md5($_GET['w']);
$hits = file_get_contents('/hit_counters/'.$url.'.txt');
$hits++;
file_put_contents('/hit_counters/'.$url.'.txt', $hits);
}
and then later you could echo out the hits under that or pull the hits in on another script and echo like that.
If you need it to create new ones on the fly, you could add something like
if(!is_file('/hit_counters/'.$url.'.txt')){
$fh= fopen('/hit_counters/'.$url.'.txt', 'w');
fwrite($fh, '1');
fclose($fh);
}
NOTE
This could end up creating a ton of tiny text files, though. So be aware. If you are worried about that, you would really need to look into a database or read in a text file line by line to find the same hash.
TO IMPLEMENT
Replace the top part of your code within the <?php ?> with the following:
if( isset($_GET) && !empty($_GET['W']) ){
$url = md5($_GET['w']);
if(!is_file('/hit_counters/'.$url.'.txt')){
$fh= fopen('/hit_counters/'.$url.'.txt', 'w');
fwrite($fh, '1');
fclose($fh);
}else{
$hits = file_get_contents('/hit_counters/'.$url.'.txt');
$hits++;
file_put_contents('/hit_counters/'.$url.'.txt', $hits);
}
}
This will take the page name in, hash it, check to see if /hit_counters/THEHASH.txt exists, and create it if not or add +1 to it otherwise. A hash is sort of like encryption, but not really. It will change your $_get['w'] into a longer random-looking string.
You're writing the same hits.txt file regardless of what $_GET['w'] is set to. Try putting the hits.txt file in a folder like this:
<?php
$url = $_GET['w'];
$dir = str_replace(".html", "", $url);
$hits = file_get_contents($dir.'/hits.txt');
++$hits;
file_put_contents($dir.'/hits.txt', $hits);
echo $hits;
?>
<iframe src="<?php echo $url; ?>"></iframe>
<p>
<?php echo $hits; ?>
</p>