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];
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>';
?>
Still learning php as I go so this might just be something I haven't gotten to yet but it's the next roadblock in building my personal site. I have a basic understanding of includes such as linking:
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
to pull my includes but I've come to a small problem in that my generic art-gallery page needs to switch between a 'portfolio' header and an 'artwork' header. So I figured I could either build "art-gallery.php" AND "port-gallery.php" and go back and relink everything or just make it so that when you call the link like the above code I just specify which header goes with it. Unfortunately this would also require going back and changing every link. But I noticed that I did state:
...&menu=side-menu-portfolio...
and the pages are already calling side-menu-artwork or side-menu-portfolio so if I could just call in menu and cast aside the 'side-menu-" portion then it would just use artwork or portfolio and call the right header. Unfortunately this is where my limited knowledge of php and syntax come in. I have tried to produce the following code based on my php and js understanding:
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
but I don't know if my syntax is just wrong or if what I'm trying to do is impossible to begin with. Note that when I try this I get
Function Include error of "Warning: include(headlines/headline-.php)"
so it looks like everything else is reading correctly, I just don't know if or how I can extract the word I want from the rest of the menu name.
Should be, Assumed your included file name is headline-side-menu-portfolio.php
<?php
$filename = str_replace("side-menu-", "", $_GET['menu']); // headline-portfolio
include("headlines/headline-" . $filename . ".php"); // headline-portfolio.php
?>
Something like this :
<?php include("headlines/headline-" . $_GET["menu"].".php"); ?>
<!--gives include("headlines/headline-side-menu-portfolio.php")-->
where
$_GET["menu"] = 'side-menu-portfolio'
Try this:
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
Your code is wrong.
Instead of
<?php include("headlines/headline-" . $_GET[menu - "side-menu-" ] . ".php"); ?>
try
<?php include("headlines/headline-" . $_GET['menu'] . ".php"); ?>
You should check if the file exists before you try including it.
if (file_exists($filesrc)) { ... }
Better yet don't let the user change the menu through a $_GET variable. Instead link to a specific page or pass a variable then decide what menu to get. Like
switch ($_GET['menu']) {
case 'side-menu':
include("headlines/headline-side-menu.php");
break;
}
Just use
$_GET['menu']
, the "side-menu-" part is already in the content of your variable passed as param.
You propably want to do an if .... else so to include one header or another based on the $_GET variable menu.
So something like this will do this:
if($_GET['menu'] == 'side-menu-portfolio') {
include 'headliens/side-menu-portfolio.php';
} elseif($_GET['menu'] == 'side-menu-other') {
include 'headliens/side-menu-other.php';
}
okay....your are almost there....just quotes missing from include syntax...it should be
include("headlines/headline-.php"); /* notice the quotes*/
so it should be
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
where $_GET['menu'] should be in the url, like:
art.php?id=image id&name=This-is-my-title&menu=side-menu-portfolio
so what's happening her ??
Upon execution of the line :
<?php include("headlines/headline-" .$_GET['menu'].".php"); ?>
$_GET is fetched from the url and replaced in the header tag, so now the header tag becomes :
<?php include("headlines/headline-"."side-menu-portfolio".".php"); ?> => <?php include("headlines/headline-side-menu-portfolio.php"); ?>
Also. may i suggest that for :
<a href="art.php?id=image id&name=This is my title&menu=side-menu-portfolio">
don't use space in the url, either replace it by - or _
I have a page with two links (text link & banner link),
that should lead to the same redirect page
(on my domain).
The redirect would be to a link that shall include a variable,
that indicates which of the two link was clicked.
e.g.:
<?php
header("Location: http://external-domain.com/?ref=[value]");
die();
?>
wheareas the "value" should be "text" / "banner" or something similar.
How can I do this?
I'm not a web programmer at all so I don't have much technical knowledge,
I guess one possible solution (which I would rather avoid) would be to give a separate id for the text link and for the banner, e.g.:
text link:
http://mydomain.com/redirect.php?id=text
banner link:
http://mydomain.com/redirect.php?id=banner
whereas redirect.php would contain:
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
But that would mean that I have to use a different id for these internal links;
I would rather find a way to use the same exact internal links (maybe for example, have each link in a "div" or a "class" and get the div/class name somehow, and have them appear as the [value].
*EDIT:
Let me emphasize again: I'm looking for a way to do this without having to use any "?id=[something]" at the end of the text link or the banner link.
Thanks!
If you're already outputting the banner/text do this for the banner
<img src="imageHere.png">
And this for the text
text here
Then catch the id get values with
<?php
$id = $_GET['id'];
if ($id == 'banner'){
echo 'The clicked link was a banner';
}
else{
echo 'The clicked link was text';
}
<?php
$source = $_GET['id']
?>
<?php
header("Location: http://external-domain.com/?ref=<?php print $source; ?>");
die();
?>
should be
<?php
$source = $_GET['id'];
header("Location: http://external-domain.com/?ref=".$source);
die();
?>
Basic concatenation.
In PHP i'm using the following code to put a banner at the top of my website.
$eventinfo = simplexml_load_file("eventinfo.xml");
<div id="eventinfo"><?php foreach($eventinfo->children() as $child){ $final = $child["name"]."...<a href='".$child["adr"]."'>more info...</a>"; } ?>
</div>
The XML doc is available at the following: http://eastsidespeedway.raceresults.co/eventinfo.xml
If you go to http://eastsidespeedway.raceresults.co/index.php you'll see that the more info... link is showing up twice. One with the correct link, and the other with a link to the same page (index.php).
Can anyone shed some light on what I'm doing wrong?
Also. If you see anything that I'm doing wrong, or you know something that's easier - let me know! This is my first time using XML/PHP so I'm kinda just wingin it. Haha.
this will work for you
<?php
$doc = new DOMDocument();
$doc->load('http://eastsidespeedway.raceresults.co/eventinfo.xml');
$title = $doc->getElementsByTagName('title');
$link = $doc->getElementsByTagName('link');
//print_r($eventinfo);
?>
<div id="eventinfo">
<?php echo $title->item(0)->getAttribute('name'); ?>
<a href='<?php echo $link->item(0)->getAttribute('adr'); ?>'>More Infoo..</a>
</div>
If you look at your source:
<div id="eventinfo">5/18/2013 - Al Smiley Memorial...<a href=''>more
info...</a>...<a href='http://www.eastsidespeedway.com/dirt.html'>more
info...</a></div>
You've got two hyperlinks- one href is blank meaning that it will redirect to the current page, check your HTML code first to see if you've accidentally duplicated the element, otherwise look at the construction of your string in the php code
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'];