Variable disappears when I log in - php

I have profile page where the profile is retrieved via GET. The index file has this:
$profile = $_GET['profile'];
When I log in on the profile page, the $profile variable disappears. Here is the form action on the login function:
<form name="login-form" id="login-form" method="post" action="./index.php">
(The $profile variable is separate of the login username.)
How could I make the page retain the $profile variable?
Thanks in advance,
John

You should use a session.
Set the variable in the first page and retrieve it in any other page.

GET and POST variables aren't preserved throughout. Only from one page to another. Unless, you specifically GET or POST the variables from page to page.
Set it in a session $_SESSION['profile'] = $_GET['profile']; and retrive it $profile = $_SESSION['profile'];
Here's a simple tutorial Session in Action

If you need to retain query string data through a form, use a hidden field:
<input type="hidden" name="profile" value="<?=$_REQUEST['profile'];">

The method="post" part of a form does not send variables the same way as a GET. Instead, it posts them in the background, invisible to the user.
If you wanted to keep the variable - you could do one of two things:
Change the form to method="get".
Change the action of the form to action="./index.php?profile=<?php=$profile?>"

The variables passed from this form is accessible only in the "index.php" page. You will be able to access it as $_POST['profile'] or $_REQUEST['profile'].
If you want to access this in all your pages you have to use session.
You need to assign the value to $_SESSION['profile'] = $_POST['profile']. After this you will be able to use $_SESSION['profile'] in all pages. Before assigning value to $_SESSION['profile'] you need to add session_start(); to the beginning of the code.
You can use unset($_SESSION['profile']); to remove value from the session variable

Related

How to keep Uploadcare image in forms?

I have a form which I want to validate with PHP. The poster upload from Uploadcare's widget is part of that form.
Sometimes users have to go backward in the form to fix stuff - an incorrect email, etc. I want to have the chosen upload file persist within that form.
This works when the back button is clicked, but I cant rely on that. I'm using PHP POST variables to reload and repopulate the form.
My question: can I use a POST variables with the Uploadcare input button to make it "remember" and reload the chosen picture the user has previously uploaded?
My current code.
<input type="hidden"
role="uploadcare-uploader"
data-preview-step="true"
data images-only="true"
name="adposter"
style="display: inline-block;" />
Is it possible to do something like:
<input type="hidden"
role="uploadcare-uploader"
data-preview-step="true"
data-images-only="true"
name="adposter"
style="display: inline-block;"
value="<?php print isset($_POST["adposter"]) ? htmlspecialchars($_POST["adposter"]) : ""; ?>"
/>
As one would with other fields? Obviously the Uploadcare input widget has its own way of doing things, hence the query.
As the documentation says:
The value of [role=uploadcare-uploader] input is either empty or CDN link with file UUID.
If you externally set the value of the input and trigger the DOM change event, it will be reflected in the widget. For example, setting it to a file UUID or a CDN link will show that file as uploaded in the widget. You can do this on a live widget or even before it's actually loaded.
So your second snippet should work correctly.
You may also want to know the way to set widget's value via JS:
var widget = uploadcare.Widget('[role=uploadcare-uploader]');
widget.value("https://ucarecdn.com/e8ebfe20-8c11-4a94-9b40-52ecad7d8d1a/billmurray.jpg");
Sorted this. It can be done with Value. Great!
https://uploadcare.com/documentation/widget/#input-value
You can use $_POST to pass on variables from the one form to another just as you described using hidden fields. But this is not very efficient and as you write the information back to the client, they could change that.
A better way to save these kind of variables is to make use of $_SESSION:
The session support allows you to store data between requests in the
$_SESSION superglobal array. When a visitor accesses your site, PHP
will check automatically (if session.auto_start is set to 1) or on
your request (explicitly through session_start()) whether a specific
session id has been sent with the request. If this is the case, the
prior saved environment is recreated.
http://php.net/manual/en/intro.session.php
So after the image was uploaded you store the image id in a $_SESSION:
session_start();
$_SESSION['imageid'] = 123456;
Then even forms later you can access the imageid simply by using the $_SESSION variable again:
session_start();
var_dump($_SESSION['imageid']);
int 123456
See the php documentation for more information on sessions: http://php.net/manual/en/book.session.php

How can i use variable on the previous page

I have a form for search user on like index.php?topic=pagename
I assign to variable what i wrote search form. So like ;
$name= $_POST['username'];
Everything okay, i can run this query on same page.
But i want to add new query under the previous query's result.
So like ;
$name have 5 euro.
Add money : NEW FORM
I can add form for add new money to under the previous query echo's. It seems. And it runs but i cant use previous variable $name .
Btw, add money form run on same page
What about just storing that value in a hidden input in the new form?
// new form
<input type="hidden" name="name" value="<?= $name ?>"/>
You can try $_SESSION:
http://php.net/manual/en/features.sessions.php
or $_COOKIE:
http://php.net/manual/en/features.cookies.php
Use sessions for this.
Store data in session, then use it in previous page.
http://www.w3schools.com/php/php_sessions.asp

Stop refresh page

I have this kind of href:
DOWN</td>
When I click on the links I lost datas entered before in a form,what is the way to prevent this?
Thanks!!!
You can either set a cookie:
setcookie('savedData' , $whateverYouWantToHaveSaved, time()+60*60*24*7);
or use a session
session_id();
session_start();
$_SESSION['savedData'] = $whateverYouWantToHaveSaved;
Ref:
cookies: http://www.php.net/setcookie
or
sessions: http://www.php.net/manual/en/ref.session.php
From there you will recall your data via either:
$savedData = $_COOKIE['savedData'];
or
$savedData = $_SESSION['savedData'];
The rest you will need to learn on your own.
You could use a hidden textbox inside the form and when you click on the link you call javascript function to set the value of the hidden input-field and that function submits the actual form. After that you could access the hidden input value from PHP.
DOWN</td>

Using $_POST information only once

I have some information which gets passed from a form and needs to be used once and only once. I can collect it nicely from $_POST but I'm not sure which is the "best" way to ensure that I can only use it once, i.e. I want to avoid the user pressing F5 repeatedly and accessing the function more than once.
My initial thought was to set a session variable and time the function out for a set period of time. The problem with that is thay could have access to the function again after the set period has elapsed.
Better ideas welcomed!
A redirect to another page would be sufficient to break most browser repost-on-refresh behaviour. Setting a cookie on form submit (or a session variable, as you suggest) would also work quite nicely. You could have the form submission page unset the session variable again, such that only a fresh access to the form would permit re-submitting the form.
This one's VERY easy to implement.
All you need to do is this:
have your form submit to a different page, which will only handle the post information, and not display ANYTHING
Then, send a LOCATION header to redirect the browser to a new page (which will be retreived by GET) This will break the browser's repost-on-refresh behaviour
You can redirect to some other page, like doing
header("Location: index.php");
How about:
1) create a page with the form, eg myformpage.php
2) Make the action of the form myformpage_submit.php
3) in myformpage_submit.php do whatever it is you need to do with the posted info, like inserting into a database.
4) When finished, direct the browser to another page, eg nicework.php
This should dispose of them as you wish.
To avoid the user refreshing the page and accessing to the information,
You can use the token method in the form like this:
<?php
if ( isset($_POST['submit'], $_POST['token']) && ($_POST['token'] === $_SESSION['token']) )
{
// do something here
}
$_SESSION['token'] = uniqid();
?>
And for the form
<form method="POST">
<input type="hidden" name="token" value="<?= $_SESSION['token'] ?>">
<button name="submit" class="btn btn-success">Submit</button>
</form>

Storing redirect URL for later use

I'm trying to store the redirect URL for use a few pages later but I'm having trouble figuring out how to get it from one place to another.
Usually I'd just pass a variable thru the URL, but since my redirect URL contains URL variables itself, this doesn't exactly work.
To give you a better idea of what I'm trying to do, here's the structure.
PAGE 1: User can click a link to add content on PAGE 2
PAGE 2: User enters text. Submitting the form on this page calls "formsubmit.php" where the MySQL data entries are handled. At the end of this I need to redirect the user to PAGE 1 again. The redirect URL needs to exactly match what was originally on PAGE 1
Does anyone have any suggestions on how to go about this?
You should use $_SESSION to store the variable in session memory. As far as specifics go with how to handle this in particular, you should be able to figure it out (store the variable, check if it exists later, if so redirect etc etc) but $_SESSION is going to be much more efficient / less messy than trying to pass things back and forth in query strings.
To declare a session variable you would do something like this:
$_SESSION['redirUrl'] = "http://www.lolthisisaurl.com/lolagain";
And then to reference it you just do
$theUrl = $_SESSION['redirUrl'];
Here is some material to get you started: http://php.net/manual/en/reserved.variables.session.php
I would recommend either using session variables, or storing the redirect url in a hidden form parameter. Session variables are pretty simple; just initialize the session (once, at the top of each page), and then assign variables to the $_SESSION global var:
<?php
session_start();
...
$_SESSION['redirect_url'] = whatever.com;
...
Hidden form parameters work by sending the data from page to page as form data. On the backend, you would add code that would put the URL to be stored in a form variable:
<input type='hidden' name='redirect_url' value='<?php echo $redirect_url; ?>';
On each page, you can take the URL out of the $_POST or $_GET variable (whichever is appropriate) and insert it into a hidden form in the next page.
You can use urlencode and urldecode to pass a string that contains elements that would otherwise break a url in a url query.
I can see two possible solutions :
get the previous page from document.referrer ([edit] find more info on this SO thread : getting last page URL from history object - cross browser?)
store the previous url via a session variable ([edit] MoarCodePlz pointed this out in his answer)
Regards,
Max
You can add this hidden field in to your form:
<input type="hidden" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
Then use header() to redirect to this page:
header('Location: '. $_POST['referer']);

Categories