Storing session in href - php

I want to store some data into a session so I can transfer it between pages. I will have a lot of images present and need an efficient way of storing data. So I've done
</td>;
However the when I call the session in another page, it says it is undefined. I could do it this way or when the image is clicked, I could store some data into a variable, call a function, save the variable into the session and then load the new page. However I do not know how I could do this. Any help will be appreciated
Thanks

1st, your html is broken, there's a </td> that should not be there.
About your question, you don't have to pass the value in the URL (which is not done that way however), you only need the session id if you don't want to use cookies, and use it to start the session.
Your link:
<a href="create2.php?<?php echo session_name().'='.session_id(); ?>">
<img src="pictures/laptops/reebok.png" alt="reebok" height="150">
</a>
In create2.php call session_start() before everything. Of course you don't need to pass the session id in the URL if you use cookies.

Make sure you're calling session_start() at the top of each of your PHP files before you try to access/modify session variables.

First of all, your php init tag is wrong <?php? should be <?php. Second, you should be initializing the session in your php:
<?php
// nothing before here, session_start must be the very first thing on the page
session_start();
And finally, you're not creating any php link that way, you are only defining the variable but it has nothing to do wint the a tag.
To send the 'choice_1' which is stored in the SESSION global variable trough URL you should do:
<a href="create2.php?choice_1=<?=$_SESSION['choice_1']?>">
<img src="pictures/laptops/reebok.png" height="150">
</a>;

Related

Passing a $_SESSION using <a href> outside php tags

I'm working with PHP and in this instance I want to pass $_SESSION which I've already assigned with my currentUser object. This works fine.
I've used the header function to pass variables between pages such as header('Location: example.php?uID='.$uID) and then using GET to receive this value.
However, instead of using a header to automatically redirect, I want to use a href so that the user has to click on a link, which then initiates the redirect.
The problem is that I use the anchor (<a>) inside the PHP tags and I can't pass variables using it inside the HTML tags.
I'd be very grateful for the solution and happy to elaborate. Thank you!
You can do it that way :
//assuming that the user id is stored in the 'uID' field of $_SESSION
Redirect me !

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

Pass url variable on to next page?

I need some help on passing a url php variable onto the next page. I've tried searching throughout the site for help and I've spent a lot of time trying to figure this out with no luck. Basically I need to be able to change the paypal link button id on page 2 with the url variable from page 1.
The variable is initially passed along with the URL: http://www.example.com?p=paypalbuttonid
I would like to store and pass that "p" variable on to the next page. I don't want to pass the variable onto page 2 with a link. I would prefer to store the variable and recall it on page 2.
Page 1 code (above html):
<?php
session_start();
$_SESSION['paypal'] = $_GET['p'];
?>
Page 2 code (above html):
<?php
session_start();
$p = $_SESSION['paypal'];
?>
I'm calling the variable in a link on page 2 (body):
<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=<?php echo $p ;?>" target="_blank" class="btn">
I'm not sure what I'm dong wrong but I'm a complete newbie to PHP so please help! The variable shows up blank in the URL on page 2. Thank you! - Chad
First, you should make sure you dont have any output before session_start(), it is safe to put session_start () at the top of the page , especially if you use php code in .html, since you may have output without awareness, and session wont work if you have anything output to the browser before session_start()
according to php.net:
To use cookie-based sessions, session_start() must be called before outputing anything to the browser.
and you should check if you cookie is enabled.
Secondly, var_dump ($_SESSION); to see if you can get anything

Making (or passing) an <a href> php variable invisibly on a link

Is there a way to put a php variable in an without it automatically being visible, like a GET variable?
Some click
I want the subsequent browser URL to be www.myphpfile.php (no variable visible).
Thanks for any help.
If I understood you right you want to have something like this:
Start a session and write the contents of $_GET['location'] into, e.g., $_SESSION['location'].
Redirect the user, e.g. header('Location: myfile.php');
If the user changes his location, start at 1
You could create a form for it with method="post" and style the submit button as a normal link using CSS.
That is if I understood you question correctly.
You should use <a href="fake" onclick="window.location=real">, but I prefer use links as The Lord W3C has declared originally.
Edit:
<a href="javascript:goto(nice_url_like_thing)">;

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