Case Studies : Passing variables between pages in PHP with PHP Session.
Needs : Webserver package, already installed .

For the case this time, assume that you will make the application logged on to the page – next page after the user is logged in, you want to continue to greet the user. Of course, you must store the variable name of the user right? Basically, there are four ways to do this, namely:
1. Pass the variable in the URL.
2. Through Session.
3. Via Cookie and,
4. With HTML Form.

This tutorial will discuss about the point to one and two of the four ways above, ie pass the variable through a combination of URL and Session.

1. Type the following script (named script1.php).

$_SESSION['username'] = "June";
$mybook = "My Name is June";
echo "<a href='mybook.php?book=$mybook'>";
echo "Click here to see your book!";
echo "</a>";

Note that in the script above, we assign a username variable manually (assumption: that the user has been logged and the username is saved in the session). So, how to store value in session is with the following syntax:
$_SESSION['username'] = "June";

Meanwhile, variable book will be passed through the URL. Its syntax is as follows:
mybook.php? book = $mybook , with variable mybook has been assigned previously.

Once executed, the script above will output the following :
Click here to see your book!

2. Type the following script (save it as mybook.php). The following script is the script from the script output link 1 above. This script will read a variable that has dilewat through URL and Session.

echo "<h2> Welcome to your Book, ";
echo $_SESSION['username'];
echo "! </h2> <br>";
echo "Your favorite book is ";
echo $_REQUEST['book'];
echo "<br>";

Note the section above. From script1.php, we know that we have missed 2 variables, namely variable book and variable username , with each – each value is My Name is June for variable book , and June for the variable username.

Then I read the second variable is:
1. Reading the variable book (which is passed via the URL): $_REQUEST ['book'] .
2. Reading the variable username (which is passed via Session): $_SESSION ['username'] .

Click the link generated script1.php, then you will be redirected to the script page 2 (mybook.php), with a view like the picture below.

Haaaapy Coding :D
Hal yang juga menarik:

Hak Cipta

Semua skrip dan teknik dalam artikel di boleh digunakan sebagaimana kehendakmu tanpa perlu mencantumkan sumber. Kamu tidak boleh mengkopi seluruh artikel, dalam Bahasa Indonesia ataupun diterjemahkan ke dalam bahasa lain.

Related Articles:

Artikel terkait:


About the author

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she lo

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

x( X-( B-) ;-) :wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :D :?: :-| :-x :-o :-P :-D :-? :-)) :-) :-( :-& :) :( :!: 8-O 8-) 8) (Русский) (yahoo) (worship) (woot) (wave) (unsure) (tongue) (thinking) (tears) (taser) (smileydance) (sleeping) (sick) (scenic) (rofl) (rock) (party) (panic) (okok) (nottalking) (ninja) (music) (muscle) (muhaha) (money) (mmm) (lonely) (lol) (lmao) (idiot) (hungry) (highfive) (heart_beat) (heart) (headspin) (hassle) (haha) (gym) (griltongue) (goodluck) (girlkiss) (funkydance) (fish_hit) (eyeroll) (evilsmirk) (evil_grin) (drinking) (doh) (devil) (dance) (cry) (cozy) (coffee) (brokenheart) (bringit) (blush) (bigeyes) (beer) (banana_rock) (banana_ninja) (banana_cool) (applause) (annoyed) (angry) (K) (: