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'];
Related
I want to access $_SESSION['roleid'] in master.php. master.php is included in every page. I'm only able to access $_SESSION['roleid'] in dashboard.php after user login. How to access $_SESSION['roleid'] in every page.
<?php
session_start();
if($_SESSION['login']==1) {
$_SESSION['loggedIn'] = true;
$role_id1 = $_GET['role_id'];
// store here in session
$name=$_GET['name'];
$_SESSION['roleid'] = $role_id1;
// $role_id=$_SESSION['roleid'];
$a=$_SESSION['roleid'];
// echo $a;die;
if(isset($_SESSION["roleid"])){
header("location:api/dashboard.php?role_id=$a?name=$name");
}
} else {
header("location:index.php");
echo "login unsuccessful.";
}
?>
To be able to access the session variables you need to call session_start(); on top of every page that will use the session variable. After the start call has been made you can use session variables like this echo $_SESSION["my_var"]; and this to set the content $_SESSION["my_var"] = "Var content";, if you are unsure what the session actually belongs it is possible to check the content of the session by doing var_dump($_SESSION);. This will show all the data the session contains since it is passed as an array.
Please do remember that a session is not recursive through subdomains because of the cookie that is being used to track which session belongs to who. A session is also dependent on that headers are not sent yet since it needs to interact with the cookies.
To delay sending of headers do this:
1. Call ob_start(); at the completely top of the scripts that needs to set multiple headers
2. Do the things you need to do like set headers and so on
3. Call ob_end_flush(); to send the headers.
Here is the offical PHP docs on this:
https://www.php.net/manual/en/function.ob-start.php
https://www.php.net/manual/en/function.ob-end-flush.php
you should check $_SESSION['roleid']:
* if having $_SESSION['roleid'], you will get it. On that code, you store $_GET['role_id'] to $_SESSION['roleid'] but $_GET['role_id'] have no in all page, it's only in dashboard.
I think that. You should try.
Here is my scenario.
I have 2 different pages with php
1). index.php page have session name declared as "session_one"
$some_session = session_name("session_one");
session_set_cookie_params(0, '/', '.domain.net');
session_start();
2). order.php page have session name declared as "sesson_random" (this is required to have another session name due to nature of implementation
$some_session = session_name("sesson_random");
session_set_cookie_params(0, '/', '.domain.net');
session_start();
now the issue that I am facing is that I have stored some values on index.php in session which I want to retrieve on order.php. I have tried many ways but unable to pass it.
Please note that I can not pass those values in query string of url.
Please help
You should be able to read the data from the other session, by restoring it before you open the new one. All you have to do is use the session_id function. I tested it with this code right here: (index.php)
<?php
session_name("session_one");
session_start();
$_SESSION["test"] = array("this is just a test");
print_r($_SESSION);
?>
Now, all you have to do is load the other session first and save the values into an array: (order.php)
<?php
if(isset($_COOKIE["session_one"])){
session_id($_COOKIE["session_one"]);
session_name("session_one");
}else{
session_name("session_one");
}
session_start();
$session = $_SESSION;
session_write_close();
print_r($session);
if(isset($_COOKIE["session_random"])){
session_id($_COOKIE["session_random"]);
session_name("session_random");
}else{
session_name("session_random");
}
session_start();
$_SESSION["other"] = array("this is another test");
print_r($_SESSION);
?>
The two sessions get combined. If you are not bothered by that, you should be good to go. Got some inspiration from here:
Can multiple independent $_SESSIONs be used in a single PHP script?
i have this code:
$username = $_POST["username"];
$password = $_POST["password"];
if(mysql_num_rows($result80)>0)
{
$row80 = mysql_fetch_assoc($result80);
$_SESSION["loginmng"] = 1;
$_SESSION["username"] = $username;
$_SESSION["password"] = $password;
$fname = $row80["fname"];
$lname = $row80["lname"];
$userid = $row80["id"];
}
and every thing is ok because i tryed to echo the session and its work in the same page (index.php)
now i have this check:
if(($_SESSION["loginmng"]!=1)||(!isset($_SESSION["username"]))||(!isset($_SESSION["password"])))
{
header("Location: index.php");
}
when i put this into new folder:
newfolder/index.php
the check is not working right,when i have logged in , and the session is set....when i am tring to echo $_SESSION["loginmng"] and the other sessions,,its values is empty like no session setted and the header is got run ...and go to index...i have put session_start(); in the first php line too
i tryed too:
if($_SESSION["loginmng"]!=1)
{
header("Location: ../index.php");
}
and the same thing...like no session set, what may be the problem
A PHP session variable is used to store information about, or change settings for a user session. Session variables hold information about one single user, and are available to all pages in one application.
PHP Session Variables
When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state.
A PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping items, etc). However, session information is temporary and will be deleted after the user has left the website. If you need a permanent storage you may want to store the data in a database.
Sessions work by creating a unique id (UID) for each visitor and store variables based on this UID. The UID is either stored in a cookie or is propagated in the URL.
Starting a PHP Session
Before you can store user information in your PHP session, you must first start up the session.
Note: The session_start() function must appear BEFORE the <html> tag.
Maybe you forgot to add session_start(); on top of the file.
To make session start on each page you need to start the session on each page.
session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.
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'];
}
I have a log in form that allows persistent login and regular session. Long story made short, when users are in their account, they can change password, email and stuff. But for that, I need to get their username from their session or cookie first (so I can do the proper SQL query).
I try to do so with this code:
if(isset($_SESSION['username']))
{
$username = $_SESSION['username'];
}
else
if(isset($_COOKIE['username']))
{
$username = $_COOKIE['username'];
}
But if I try to echo $username, I keep getting "undefined variable". Why is that?
I noticed that if I put a session_start(); at the top. I get the proper username for session but not for cookie of course. How can I solve that?
The weird part (for me) is that I got the exact same code (well that part) in another page and username isn't undefined.
PS: If something isn't clear or more information is needed, please tell me.
EDIT
I tried this:
function accountValidation()
{
if(isset($_SESSION['username']))
{
$username = $_SESSION['username'];
}
else if(isset($_COOKIE['username']))
{
$cookie = $_COOKIE['username'];
$explode = explode(' - ', $cookie);
$username = $explode['0'];
}
echo $username;
}
accountValidation();
And it worked ... So if I put it into a function and then call it, it works?! What is the diference? Why does it need to be into a function for it to work???
If you set certain cookie, it would be available to you from next reload. As $_COOKIE is set when a page head is called. You wont be able to retrieve the cookie from the same page which has set the cookie. I hope you got what i meant. If not let me know I would give an better example.
EDIT:
Example
<?php
session_start();
$_SESSION['test'] = 'test1success';
echo $_SESSION['test'];// would display test1success
if (!isset($_COOKIE['test2']))
{
setcookie("test2", "test2success", time()+3600);
}
echo $_COOKIE['test2'];
// wont display test2success when you load the page for first time
// reload it & it would display test2success
?>
Explanation:
The first thing you need to understand is that the cookie is stored on your PC(browser) when the page is loaded. The client (i.e. browser) sends cookie headers to the server & does the page execution. The values set by set_cookie during page execution are set on the client pc, and the server doesn't know about the new values just set - unless you reload the page & the cookie header is sent back.