Saving page ID within $_SESSION - php

I am new to sessions, and think I get the basics of them, they seem to act like containers holding information which you can use on at a later stage and are linked to your UID.
I am wondering how I would save the ID of a page (example ID123) on the click of a button, and what exact code I'd have in the header.
Much appreciated in advance!
EDIT:
This is on wordpress, sorry to add this, each page has an 'event ID' I want this to be stored for use later (at a kind of checkout page) sorry for not adding this!

Saving page ID within $_SESSION
session_start();
$_SESSION['pageId'] = (int) $_GET['ID'];
Now you can use $_SESSION['pageId'] to get its value where you need.
Make sure to put session_start(); at top of your script where you use session-related functions or $_SESSION array.
To unset it when you don't need it anymore, you would do:
unset($_SESSION['pageId']);
session_destroy();
session_regenerate_id();
update
On button click you would do something like this:
var btn = document.getElementById('btnId');
btn.onClick = function() {
window.location = 'saveSession.php?id=xxx' // replace xxx with your id value
};
Now in saveSession.php you wll have to use code like shown above for storing it in session.
You can also use Ajax though.

In wordpress you cannot pass custom url parameters. So you cannot send something like
http://yourwordpresswebsite.com/?custom_param='value'
WOrdpress doesnt allow it. SO if all you want is a Post ID or a Page ID. It is easy to grab in wordpress. Do:
$val= $post->ID
Thats it and $val is set. The way I do if I have to send any custom parameters to a different file is by sending it as a form variable. There might be better ways but I am new to Wordpress and php too. So this is what I do:
<?php
echo "<form action='php_file.php' method='post' name='form_name'>
<input type='hidden' name='eventid' value='$event_id' />
<input type='submit' name='submit' value='submit'/>
</form>";
?>
If you want this to be your session info you can just add it in your php file where you are collecting the above fields.
session_start();
$_SESSION['pageId'] = (int) $_GET['eventid'];
/* Do not forget to check if the get variable is clean before you perform any operations, use mysql_real_escape_string(). It is a very important security measure. */
This might not be the best approach as I told you I am new to wordpress too.

Related

is it possible in php to save changes made on web page without sending into database

I have done some research on this topic but couldn't come up with a solution.
Is it possible in php to save changes made on web page without sending the infos into database??I'm giving each user the ability to make some changes on their pages and after changes made,every visitor of that page would be able to see the new edited text.
How can I do that?
Here is the sample php code
<?php
if(isset($_POST['x']) && isset($_POST['hi'])){
$hel=$_POST['hi'];
}
else{
$hel="Hello There";
}
?>
HTML Part
<form id='sub' name='sub' method='post' action='edit.php' enctype='multipart/form-data'>
<input type='text' id='hi' name='hi'>
<input type='submit' id='x' name='x' value='publish'>
</form>
<?php echo $hel;?>
Thanks in advance.
No,It is not possible.That is what database for.Use database to store the changes,because that is the easiest and best way to do it.
Or you can store it as a file as well.This link might be useful,if you are not familiar with file handling.
http://davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete
Yet again,using database is better choice.
You have to save it somewhere in permanent storage. You get to choose - either a database or file. So if you don't want to store it in a DB, then store it in a file.
However when your data becomes complex and you want to link data to each other, you really need to go to a database.
Probably not what you're asking, but you can save data until you close the browser using sessions.
Start your script with:
<?php
session_start();
Then, save data inside $_SESSION:
$_SESSION['x'] = 'Whatever';
$_SESSION['hi'] = 'Great!';
Everything inside $_SESSION will stay even if you reload the page, until you close the browser.
If not, write bunch of JSON files to the filesystem.

passing id of an image to another page

This might a little noobish but I don't know where to start with my problem. A friendly nudge in the right direction would be greatly appreciated.
I am trying to have a gallery of material, the images of the materials are stored in a database, now i want to make the users click on a specific image of the material that they like, this would lead them to next page that would calculate the price.
So i guess my question is, how do i carry this variable of their choice to the next page from the gallery?
Any help greatly appreciated
btw this is my code for getting the images displayed from the database:
<?php
mysql_connect("localhost","root","");
mysql_select_db ("imagez");
$res=mysql_query("select * from imgz");
while($row=mysql_fetch_array($res))
{
echo "<a href=\"page.php?id={$row['id']}\" />
<img src=\"{$row['img']}\> </a>";
}
?>
you can hand over values to another page by...
(1) QUERYSTRING (URL)
page.php?do=show&id=5
create those by pulling the unique id of a record from your db and create a link:
echo "<a href=\"page.php?id={$row['id']}\" />{$row['name']}</a>";
assuming that your table imgz has the fields id, name.
in page.php, you get those variables with
$_GET['do']; $_GET['id'];
pro: quick & easy
contra: can be easily manipulated by evil users - they change the querystring and get your script to do weird things.
(2) POST
create a <form method="post" action="page.php">with a hidden field named id containing the 'id' you want to pass. Add a <submit>-button to send the data to page.php.
in page.php, you get those values by:
$_POST['id'];
pro: safe, because evil user can't fiddle with POST
contra: overhead
(3) SESSION:
call session_start(); on top of every page, write your data into the session by...
$_SESSION['id'] = $id; $_SESSION['do'] = "show";
these variables will be kept between pages, in page.php you can simply use S_SESSION['id'] and have that value - if you called session_start().
pro: easy and safe, PHP will do the work for you (e.g. handling cookies etc.)
contra: will put load on your webserver if traffic goes up on your site.
You can set URL parameters, so when they click it, you would have a parameter like;
http://example.com/pricecheck.php?photoid=435
Then on the page you would do;
$photo = $_GET['photoid'];
Then you would have the ID of that photo on the page
Instead of passing the variable via URL, you can use cookie to store the variable and doing the calculation to check up.
<?php
$expire=time()+60*60*24*30;
setcookie("price", "$54", $expire);
echo $_COOKIE['price'];
?>
Now that variable $price would be available for each your webpages, and finally don't forget to clear the cookie `setcookie("hp","", $expire);

PHP going back and keeping arguments

I have used this page http://www.binarytides.com/blog/php-redirect-go-back-to-previous-page/
to go back
but from
http://page.co/test.php?item=26
I post something to post.php and then call the php Go back function but I go back to
http://page.co/test.php
losing the argument path, any idea?
In Your form fill in the query string to the action attribute, like this:
<form action="?item=26" name="myform">
...
</form>
and after the submission Your HTTP_REFERER will contain this query string so redirect to it will be successfull...
EDIT: If the form is on the page post.php, it is enough to use action="?item=26" - of course You can and should use PHP to write down the number/ID of item from whenever it may come...
Lets say Your item ID is stored in the variable $item_id - then Your action will look like this: action="?item=<?php echo $item_id; ?>".
You should use sessions for stuff like this. Set a session when posting the data and you're set.

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']);

How to set a session variable when clicking a <a> link

I have the following problem... I want to set a session variable when clicking on a normal link like:
home
My research seems to point out that it is not possible for PHP to catch up with the click event in a such a way that it would set a session variable.
I believe it is possible with Ajax, but how? And what would my link look like?
Setting the session variable should look like:
$_SESSION['link'] = home;
So in short: When clicking on a link in HTML, a session variable must be set.
HOW am i going to do that?
PS: I'm not quite familiar with Ajax, but I'll catch up.
EDIT: The links will refer to the same page, also i want to avoid urls like "home.php?link=X".
If it isn't possible to do it any other way, too bad. But I'll hope there is a solution.
Important: the name of the link will be the value of $_SESSION['link']
session_start();
if(isset($_SESSION['current'])){
$_SESSION['oldlink']=$_SESSION['current'];
}else{
$_SESSION['oldlink']='no previous page';
}
$_SESSION['current']=$_SERVER['PHP_SELF'];
Maybe this is what you're looking for?
It will remember the old link/page you're coming from (within your website).
Put that piece on top of each page.
If you want to make it 'refresh proof' you can add another check:
if(isset($_SESSION['current']) && $_SESSION['current']!=$_SERVER['PHP_SELF'])
This will make the page not remember itself.
UPDATE: Almost the same as #Brandon though...
Just use a php variable, I know this looks like a security risk, but when done correct it isn't.
Register Now!
PHP:
if(isset($_GET['a']) /*you can validate the link here*/){
$_SESSION['link']=$_GET['a'];
}
Why even store the GET in a session? Just use it.
Please tell me why you do not want to use GET. « Validate for more security.
I maybe can help you with a better script.
I had the same problem - i wanted to pass a parameter to another page by clicking a hyperlink and get the value to go to the next page (without using GET because the parameter is stored in the URL).
to those who don't understand why you would want to do this the answer is you dont want the user to see sensitive information or you dont want someone editing the GET.
well after scouring the internet it seemed it wasnt possible to make a normal hyperlink using the POST method.
And then i had a eureka moment!!!!
why not just use CSS to make the submit button look like a normal hyperlink??? ...and put the value i want to pass in a hidden field
i tried it and it works. you can see an exaple here http://paulyouthed.com/test/css-button-that-looks-like-hyperlink.php
the basic code for the form is:
<form enctype="multipart/form-data" action="page-to-pass-to.php" method="post">
<input type="hidden" name="post-variable-name" value="value-you-want-pass"/>
<input type="submit" name="whatever" value="text-to-display" id="hyperlink-style-button"/>
</form>
the basic css is:
#hyperlink-style-button{
background:none;
border:0;
color:#666;
text-decoration:underline;
}
#hyperlink-style-button:hover{
background:none;
border:0;
color:#666;
text-decoration:none;
cursor:pointer;
cursor:hand;
}
In HTML:
home
Then in PHP:
if(isset($_GET['link'])){$_SESSION['link'] = $_GET['link'];}
Is your link to another web page? If so, perhaps you could put the variable in the query string and set the session variable when the page being linked to is loaded.
So the link looks like this:
home
And the homge page would parse the query string and set the session variable.

Categories