Session value doesnt pass on to the next page - php

Basically I want to grab an id send via the url (ex. www.website.com/?id=432432) and take it accross my website till the user hits the contact page. I created a variable and a session variable
session_start();
$getId = $_GET["id"];
$_SESSION['session_browser_test'] = $getId;
$adv_id = $_SESSION['session_browser_test'];
and used
echo $adv_id;
on my index.php Joomla template so it applies to all the pages.
But the issue is when i go to www.website.com/?id=432432 it echos the id on my web page, but if I click on the next link to go to another page (ex. www.website.com/nextPage) it doesnt hold the session value from the previous page. Why is that? and how can I carry the ID through out the site?

you will not get an id from URL on next page, likely
echo $getId;
instead you need to use id from session like,
$_SESSION['session_browser_test']; // your id stored in session

Start the session in each page
session_start();
In order to access the variable in a session, you have to call the $_SESSION variable.
echo $_SESSION['session_browser_test'];

HTTP is stateless, so you have to do something to remember your variable throughout the website .
make sure you correctly use session , like session_start();
when you send your id through get method ,it works, but when you go to any other page ,it doesn't make any sense to remember this.
use this for send id through pages:
<?php echo get_permalink(910); ?>?userid=<?php echo $value['userId'];?>
send this in url and use on next page as:
$sql = "select * from `wp_pelleresuser` where userId =".$_GET['userid'];
using this approach you can use a single variable on every page you want without using session. try google to how wordpress manage variable through all pages without using session. it will help you.
happy coding!

Start
session_start();
(if not started) in the index.php in root of your app (session probably will start on every pages) and then call (when desired):
$_SESSION['session_browser_test'];
instead of assi8gning this sess var to your own variable and then calling it in different places.

if(isset($_GET["adv_id"])){
$_SESSION['session_browser_test2'] = $_GET["adv_id"];
$adv_id = $_SESSION['session_browser_test2'];
}
else {
$adv_id = $_SESSION['session_browser_test2'];
}

Related

Login Page with session

I am storing data into database,with customer id.The insertion will be done only if the customer is logged in.if the user moves to any other page and comes back to same page the customer id disappears .so i need to include customer id in every page and how do i do it.the login page must be in last part.
Once the authentication (ex. login with a mail & password) finishes and successful, you can store the user id in the session Global Variable like this: $_SESSION['user'] = $id
And as long as the session is alive, you can retrieve this id from this Super Variable on any page: $user_id = $_SESSION['user']
Make sure to use session_start() before using the session global variable. Like:
<?php
// index.php
session_start();
echo $_SESSION['user'];
If you're not using front controller pattern then you have to use this function on every page before using $_SESSION.
You must start the session using session_start() in every page at the top. refer from below link.
https://www.w3schools.com/php/php_sessions.asp

Get ID from URL and store it in variable

I need to get the data from URL, example domain.com/?id=username
Username will vary from one user to another... Once they visit the website with link like that, they can move around the website and then at some point fill out the form. Since they moved around the website, the url will not have ?id=username in the path, so I need to store that data in the variable to be able to send it with the form.
I assume I need to set and store the cookie per session (so that cookie will refresh after session / browser exit)
I use ob_start() since I have to implement this code in the body, when the headers are already sent.
ob_start();
session_start();
$affid = $_GET['id'];
setcookie('affid',$affid, 0, "/");
$finalaffID = $_COOKIE['affid'];
ob_end_clean();
echo '<span class="testoutput">'.$finalaffID.'</span>';
After some attempts, I got this code, but it doesnt store the value after I move around couple pages, it only shows the on initial page visit.
Any ideas please?
You could use session variables.
$_SESSION["id"] = $_GET["id"];
this session var will be accessible anywhere the session is open. Just call it with $_SESSION["id"].
index.php
Url: www.domain.com/?id=user
<?php
session_start();
if (isset($_GET["id"])) {
$_SESSION["id"] = $_GET["id"];
}
?>
otherpage.php
Url: www.domain.com/otherpage.php
<?php
session_start();
if (isset($_SESSION["id"])){
echo $_SESSION["id"];
}
?>
Jose is right about saving IDs in sessions. There's a good post about it that deals SPECIFICALLY with IDs here: Cookie vs Session for Storing IDs
But, if you want to store it as a cookie, this code stores the ID.
$id = $_GET['id']);
setcookie('id', $id);
And this code allows you to retrieve the ID!
echo $_COOKIE['id'];

passing varables' value from one form to another in php

I have three forms - payment.php , payment1.php and paydb.php . payment.php contains the front end form.payment1.php contains the back end of the form of payment.php. whereas we are shifting to paydb.php from payment1.php. Now I'm filling the form by entering member number in payment.php which is retrieved in a variable $member_no in payment1.php .Now I want to get the value of member_no in paydb.php . How to do that ?
After receiving $member_no in payment1.php redirect to paybd.php with a get array
using
header('Location: http://www.example.com/paydb.php?member_no=$member_no');
then receive $_GET['member_no'] number and assign to a variable
example:
$member_no = $_GET['member_no']
The first thing is make sure you are not passing sensitive information where the public can see it. Such as in a URL.
As soon as you get the member's number... store it in a session variable.
You can probably do this when they log in.
session_start();
$_SESSION['member_no'] = $member_no;
OR on the first payment page (assuming 'member_no' is the name of the form element being passed) like this...
$_SESSION['member_no'] = $_POST['member_no'];
Now that session will persist as long as the visitor has their browser open and you don't have to worry about passing it from page to page.
You can use that session on any subsequent page simply by calling it.
<?php echo $_SESSION['member_no'] ?>
Without showing this information to the public.
ALWAYS make sure you place this at the top of any page where you want to use session variables.
if (!isset($_SESSION)) {
session_start();
}

$_SESSION variable in php (page to page)

Whenever I go to a page i.e. login page or any other page, I want to save the name of the page in a $_SESSION variable.
login page:
<?php
session_start();
$_SESSION['page'] = 'login.htm';
?>
It works only for the login page and doesnt overwrite in other pages for e.g. home page:
<?php
session_start();
$_SESSION['page'] = "home.htm";
?>
I need the sesssion variable 'page' to hold the last page I was, can anyone help please?
Why not just use $_SERVER['HTTP_REFERER']? This will give you the previous page in PHP, without having to add anything to sessions.
when you navigate to a new page first retrive the saved "back" variable (and use it in a back link/breadcrumbs or something), and then overwrite the sessions "back" variable with the curent page, to have it ready for the next move =)
If all you need is default "back" functionality you should let the browser handle it.
If what you want is something to be used as a breadcrumb following some internal order (or path in a tree) my advice is to let each page "know" the path that leads to it.
If you really need to know from what page the user came from save it to a previous variable before you write over the current variable.
// Make sure user didnt just refresh the page
if ($_SESSION["current"] !== "currentPage.php") {
$_SESSION["previous"] = $_SESSION["current"];
$_SESSION["current"] = "currentPage.php";
}
You're using different keys.. 'page' and 'back'.

How can I pass a unique ID in a url to other pages on my site?

Client wants to try some retargeting with banner ads (using Google adwords) and is having us keep the lead sources seperate. Lead source ID is submitted with the form the user will fill out. I want to create a unique id for the retargeting banners like so: http://www.mysite.com/index.php/?id=12345 and then pass that id on to other pages so when they eventually end up on the form and submit it, I can take that id and submit it with the form to the database to track the lead source (which will be associated with the id).
How can I do this?
Hopefully that is not confusing! I am new to PHP and things like this in general, so any help is much appreciated.
Edit: Here is the code that I am using.
index.php
<?php session_start();
include("includes/dynamicVars.php");
if (isset($_GET['id']))
{
$_SESSION['leadID'] = 'MICROSITE_RETARGET_CROSS';
$siteCode = $_SESSION['leadID'];
echo "Retarget ID set!";
}
else
{ echo "Default PPC ID set!";}
?>
$siteCode is defined as 'MICROSITE_PPC_CROSS' by default in dynamicVars.php
Then Page2.php
<?php session_start();
include("includes/dynamicVars.php");
echo $siteCode;
?>
And I still get the default $siteCode, instead of the new one that is set on index.php.
Right, when you go back to the index.php page, it no longer has the ID in the URL, which means the if statement goes straight to Default PPC. So what you should do is create another SESSION or you could use a COOKIE that let's you know the visitor is the same person. Example:
if(isset($_SESSION['visitor'])){
// do nothing
}else{
// create the SESSION for leadID
if (isset($_GET['id']))
{
$_SESSION['leadID'] = 'MICROSITE_RETARGET_CROSS';
echo "Retarget ID set!";
}
else
{ echo "Default PPC ID set!";}
}
// write session for new visitor
$_SESSION['visitor'] = "currentvisitor";
}
I'd suggest to take the ID and put it in a $_SESSION['leadID'];
If you want to use it with JavaScript, you should put it in a cookie.
ok I took a look at this. On the second page, instead of "echo $siteCode", do "echo $_SESSION['leadID']". and then you can set "$siteCode = $_SESSION['leadID']" on this page.
I think $siteCode is not global unless you put it in the include file and have it appear everywhere. but Using Session is known as a super global array.

Categories