I am trying to make a login form with PHP, and I want to get a value from a form like so:
$name = $_POST["name"];
$email = $_POST["email"];
then, when the user clicks "Submit", I want the new page to have access to these variables. Using the
include 'file1.php';
technically works, but I dont want ALL of 'file1.php', just the variables.
Any Suggestions?
You have to use session. Here is a simple demonstration:
page1.php - Load page one to set the session variable
<?php
// begin the session
session_start();
// set the value of the session variable 'foo'
$_SESSION['foo']='bar';
// echo a little message to say it is done
echo 'Setting value of foo';
?>
page2.php - Load page two and it will have access to the session variable you set in page1.php
<?php
// begin our session
session_start();
// echo the session variable
echo 'The value of foo is '.$_SESSION['foo'];
?>
This is exactly what sessions are for.
Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data.
Related
<?
session_start();
$_SESSION['name'] = "$_GET["name"]";
$_SESSION['email'] = "$_GET["email"]";
$session_id=session_id();
echo"$session_id <br> $_SESSION['name'] <br> $_SESSION['email']";
?>
I am trying to create a session to store visitor input form with GET method, I cant use POST because the form is handled by wordpress plugins and the client only gave me the GET option. The problem is:
On page #1, this is the page after we submit the form, the echo is shown complete.
At page #2, I already add session_start(); at top but $_SESSION['name'] and $_SESSION['email'] keep empty (change page) but $session_id is stored and show same.
What am I missing? or maybe $_SESSION is cant store $_GET?
So on the first page you save the values from the user input and it works as expected.
However, when you go to the second page, your code is overriding the session with new variables, which are empty in this case. Before assigning anything to the session, you should check whether it is not empty / valid.
For this simple code I would recommend checking via isset / array_key_exists functions.
I am having problem in maintaing value of variable through several pages(actually through same page but through many refresh).
The first time I get navigated to new page the value of variable is preserved and can be used by echo,but after refreshing the page that value cannot be reused,it shows error that the variable has no value.
I am making a webapp for chatting in php.
I want to show the name of user(sender) on every page(every page of sending message). So I am using code
<?php
$writtervar = $_POST['writter'];
echo $writtervar;
?>
I am taking input through a separate page,code is
<form action="ddd.php" method="post">
Enter your name <input type="text" name="writter" >
<input type="submit" id="submit" value="Press" >
</form>
HTTP is stateless. $_POST array is populated when a user makes a request. If you want to have access to a value accross web views, read on setcookie or sessions.
If you don't want to use cookies, you'll need to resend your parameters on every request (probably obscured some way). Or send an identifier on every request an keep your info server (you can do that with php sessions anyway). But doing that is not convenient nor secure.
You can use session. base on your code you can try this:
In start page (ddd.php) you have to set your session values.
<?php
session_start();
$_SESSION["writer"] = $_POST["writter"];
?>
...
in other page use your session values as e.g:
<?php
session_start();
...
echo $_SESSION["writer"];
?>
Note that unset and destroy your session at the end off your work.
<?php
// remove all session variables
session_unset();
// destroy the session
session_destroy();
?>
You can set a cookie
$value = 'something from somewhere';
setcookie("TestCookie", $value);
and get the value:
echo $_COOKIE["TestCookie"];
You can also use sessions:
session_start();
// Set session variables
$_SESSION["TestSession"] = $value;
// Get session
echo $_SESSION["TestSession"];
You'd better use a framework like Laravel that can help you handle sessions, forms, redirect with values etc.
You can find the documentation on php.net
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'];
I can pass values form one page to another but I need to pass value like this,
Page 1:
Page2.php
Page3.php
I need to pass the radio button values in the Page1.php to Page2.php, and also i need to use same session . if the form is redirected to page3, I am unable to get the value of page 1. its online quiz project. I tried session, form post method and few other methods but I am yet to succeed.
I would be very happy if you can help me with the code or some suggestions.
Thanks!
The best practice would be to go with OOP. So, you can do the following:
Create a class with fields to hold all the information you want to pass.
class PageInfo
{
var $pageTitle;
var $currentPage;
var $btnValue;
.....
}
2.Now crate an object of PageInfo class, and assign the values you want to set.
$page = new PageInfo();
$page->pageTitle = "Home";
$page->btnValue = 1;
...
3.Assign this object (holding all the details of your page) to the super-global session variable.
// store session data
$_SESSION['page'] = $page;
4.Now you can access the value stored in the session at the different pages.
$otherPage = $_SESSION['page'];
echo $otherPage->pageTitle;
echo $otherPage->btnValue;
Note: The session_start() function must appear BEFORE you print anyting on the page.
Actually your question is vague, first of all when you are using sessions, you need to be sure you are calling session_start() at the very top of the page, secondly, you can save the form data in your session variable like
if(isset($_POST['YOUR_SUBMIT_BUTTON_NAME_HERE'])) {
$store_temp_val1 = $_POST['whatever']; //Sanitize the value first
$_SESSION['page_one']['first_val'] = $store_temp_val1;
}
Now you can simply retrieve the value stored in the session variable on the second page like
echo $_SESSION['page_one']['first_val']; //Will echo the previous page value
Note: Use session_start() on each page at the very top.
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'];
}