Post information from 1st page to 3rd page - php

I need to consider something in the future after I finish my application. What is going to happen is that a teacher will need to first login (login page is page 1), after login the page will go straight to a menu where the teacher selects a hyperlink link to open up a page (hyperlink menu is page 2), On third page I want a message displaying welcome to the teacher who has accessed the page (page 3 is welcome page).
Is there a way to get the teacher username from page 1 and displaying it on page 3 is what I am asking?
Below is coding and example:
Page 1: InputTest.php
<body>
<form action="InputTest2.php" method="post">
<p>Please enter your name</p><p><input type="text" name="user" /></p>
<p><input type="submit" value="Send" /></p>
</form>
<!-- The above allows a name to be entered and submitted to "InputTest2" by clicking on send button -->
</body>
Page 2: InputTest2.php
<body>
<p>Welcome</p>
<p>...</p>
<p>...</p>
</body>
Page 3: InputTest3.php
<body>
<?php print "Welcome <b>".$_POST['user']."</b><br/>\n"; ?>
</body>

in your InputTest2.php
add the following code in the start of the page
session_start();
$_SESSION['usernameLogged'] = $_POST['user'];
and then you can print it like this
also you should start a session
<?php
session_start();
print "Welcome <b>".$_SESSION['usernameLogged']."</b><br/>\n";
?>

Save it in the $_SESSION superglobal.
$_SESSION['user'] = $_POST['user'];
Then $_SESSION['user'] will be available on the third page.
Note: you will need to have session_start(); on both pages in order to store/retrieve session data like that.
Possible duplicate of: php: Save the entire $_POST variable in the session

Related

How can I detect the last visited page with php?

I am on my page index.php. From here I go with a link to myform.php.
myform.php:
<?php
echo "My last visited page is:".$_SERVER['HTTP_REFERER'];
?>
<form action="success.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>
The result I see on my page is:
My last visited page is http://www.mypage.com/index.php
Now I submit my form and come to success.php. Here I click my "back" button and come again to myform.php.
The result I see is now:
My last visited page is http://www.mypage.com/index.php
But what I expect here is:
My last visited page is http://www.mypage.com/success.php
You can use sessions..
Bottom of the success.php you can assign page name to sessions
$_SESSION['page'] = $_SERVER['HTTP_REFERER'];
When click on back button you can get that value using sessions(index.php),
echo $_SESSION['page'];
On that page at the bottom, you can assign current page name to session, then it can track on next page
The back button does strange things with variables and page refreshing. Try typing the http://www.mypage.com/myform.php address into the browser and seeing what happens.
N.B. I know this only deserves to be a comment, but reputation is a pain.
As you need to carry the previous info to forwarded page. Then it will be better if you use session. When you are visiting a page, store this current page in a session variable. Add this line at bottom on each page.
$_SESSION['page'] = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
And to echo
<?php
echo "My last visited page is:".$_SESSION['page'];
?>
So whole text will look like:
<?php
echo "My last visited page is:".$_SESSION['page'];
?>
<form action="success.php" method="post">
<p>Your name: <input type="text" name="name" /></p>
<p><input type="submit" /></p>
</form>
<?php
$_SESSION['page'] ="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
?>
edit: Be careful when you are using multiple tab. Only latest info will be stored.
One way is, you can handle this by using PHP's _SERVER global, If the user first time visits your site, there will be no previous page, except that, the previous page will be last visited page identified by _SERVER, check out this script,
isset($_SESSION['current']) || $_SESSION['current'] = '';
// check for first visit to any page, initialize
if($_SERVER['SCRIPT_NAME'] != $_SESSION['current']){
// check if current page != previously recorded page
// order is important here in two lines, check it yourself
$_SESSION['previous'] = isset($_SESSION['current']) ? $_SESSION['current'] : '';
$_SESSION['current'] = $_SERVER['SCRIPT_NAME'];
}
You can make something like this, which ignores same page if user refreshes the page, disregarding the method being GET or POST.
You can include this script in common file to record the activity.
Array
(
[current] => /session/index.php
[previous] => /session/submit.php
)

Include a php page to password protected the other pages content and showing the user logging in info in the header.php

I would like to include a PHP page to protect every single page and after you login, it would show your username on the top right corner,
also it limits the only data that login user had provided such as his CV and personal info but without seeing other users info.
the structure will be the same as the previous post
index.php (include header.php, content.php and footer.php)
The title on the header.php will be changed menu after user login
Thank you.
Regards
Andy
Well if you want a script to ensure that it would be something like this:
first: Assuming you're not using design patterns and it is a php basic project with an archetype like " scripts(folder) css(folder) js(folder) index.php header.php and footer.php". lets create "security.php".
<?php
session_start(); //starting session to acces to it
if(empty($_SESSION["username"])){// if there's no data username in session
header("location: ./login.php"); //go and take them out of here!
}
?>
Now you have "security.php" ready you just have to include it to your protected pages and create a "login.php" page.
Ex: For including security.
<?php
//#mySecurePage
include "security.php";
//My Page Content Code or event header code if you want validation after loading anything (Best)
?>
Second: Create a Login page like.
<?php
if(!empty($_POST["username"]) && !empty($_POST["password"])){// if all data from login was sent
if($_POST["username"] == "me" && $_POST["password"] == 1234){//your validations here
session_start();//start session to acces it
$_SESSION["username"] == $_POST["username"];//allocate username
header("location: ./securedPageHere.php");//forward to a securedPage
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My Login Page</title>
</head>
<body>
<form class="" action="" method="post"><!-- action is empty to send data to same page (login)-->
<input type="text" name="username" value="" placeholder="username">
<input type="password" name="password" value="" placeholder="password">
<input type="button" name="login" value="Login">
</form>
</body>
</html>
Now we're almost done, just need to load username at your menu bar
Third: Load username at menu bar. Just add the value accesing session (remember if you have loaded "security.php" you've already started session
<div><?php print($_SESSION["username"]); ?></div>
Now to use the Logut Button you have to destroy session, so add a listener to it and then just execute a script like:
<?php
unset($_SESSION); //clear session array
session_destroy(); //Destroy session
?>
Hope it helps you.
EDIT: to limit data accessed just use the username (best id) data from the login verified data saved at session array :).

Using PHP to change pages content

I have a few pages, PAGE 1 is an input form that allows accesed users to change PAGE 2/3 etc.... I am hoping to use PHP to do this but im not too PHP savvy.
Scenario
Page 1 has two drop down menus that are made in HTML (master page) the first has the persons name "Mike" & "Jim" and the second contains a score from 0 to 10. I want that if i select mike and 7 and then click submit, it will update page 2 (mikes page) and display "Mike" & "7" and then if i click "Jim" & "3" it will update jims page to display Jim and 3
How will i most efficiently go about doing this? preferably simple PHP if its possible.
All these pages + scripts will be on a server & i know jscript only is client side meaning it cannot change another page.
Any help is greatly appreciated
you can achieve this by creating sessions. for example, there i have taken 2 pages and sending data from input field of one page to another
page1.php
<?php
session_start(); //this must be the first line in php page
$_SESSION['variable']=$_POST['anything']; // this will save value from input tag in form in session.
header( "Location: page2.php" );
?>
<html>
<head>
<title>Session</title>
</head>
<body>
Welcome to page 1.
<br/>
<br/>
<form method="post" action="page1.php"> // this will call page2.php on button click
Type anything
<input type="text" name="anything" />
<input type="submit" value="Send value to page 2" />
</form>
</body>
</html>
page2.php
<?php
session_start();
echo $_SESSION['variable'];
?>

PHP - How to send values in Multiple pages Form

So, I was working on a PHP Application Center (Form), and I'm stuck at something.
I have 3 pages of form and 1 sumbit page. All are in .php extention. So, I want to take the <input name="name" type="text" placeholder="Name" required> from page one and:
<label>About yourself (Atleast 50 Words): <span>*</span></label><br />
<textarea name="yourself" placeholder="About yourself (Atleast 50 Words)" ></textarea> to the submit page.
I can take the values of Textarea & Textbox from page 3 to submit page through <?php echo $_POST['somethinghere']?>. But I can't take values from the First and second page.
Here are links to my php codes with style.css (pastebin.com):- http://pastebin.com/81vgHh5H
I had a look at the Pastebin and I think I can see the problem. You're setting the Session data on the same page that they're filling out (so you're assigning those fields to the session before they've even been filled in). You need to do that on page 2, not page 1. So your pattern is thus:
Show page 1 form.
User fills it out and hits submit.
Send user to page 2.
Page 2 grabs the information posted from Page 1, stores it in the session.
Page 2 shows the second form page.
User fills out page 2 and hits submit.
Send user to page 3.
Page 3 grabs the information posted from Page 2, stores it in the session.
And so on...
By the time you get to the last page, your session will contain all the information from the form and you can then process it however you need to.
Example:
Page 1:
<form action="page2.php">
<input type="text" name="page1text"/>
<input type="submit"/>
</form>
Page 2:
<?php
$_SESSION['page1text'] = $_POST['page1text'];
?>
<form action="page3.php">
<input type="text" name="page2text"/>
<input type="submit"/>
</form>
Page 3:
<?php
$_SESSION['page2text'] = $_POST['page2text'];
?>
<form action="page4.php">
<input type="text" name="page3text"/>
<input type="submit"/>
</form>
Page 4:
<?php
$_SESSION['page3text'] = $_POST['page3text'];
?>
<ul>
<li>Page 1: <?php echo $_SESSION['page1text'] ?></li>
<li>Page 2: <?php echo $_SESSION['page2text'] ?></li>
<li>Page 3: <?php echo $_SESSION['page3text'] ?></li>
</ul>
As you can see, page 4 winds up with access to all of the information from the previous pages.
Edit: This answer previously read: I would recommend having a model that contains all of the form's properties. As they fill out each page, populate the model with the data the user has entered and then when they successfully complete the form you can destroy the session data.
You can store them in session like, pass your data from page one to page two and in page two do following
$_SESSION['name'] = $_POST['name'];
Again, pass your data from page two to page three and in page three do following
$_SESSION['yourself'] = $_POST['yourself'];
This will set your name and yourself data in session and will be accessible from any page. Do not forget to include session_start() at the top of your page if that page uses session.
Now, you can access those session variable from submit page like,
echo $_SESSION['name'];
echo $_SESSION['yourself'];

How to use http_referer if form field and php script are at the same page?

Suppose i am using pure php, with no javascript/jquery or ajax.
I have many pages in a website, lets say page1, page2, page3 and page4.
all of the first three pages have a link to go to page4, to log in.
In page 4 i have a form field, and above I have a php script to catch the user input and put the username in a session and after that i want to redirect to the page where the user came from, but the page is not redirecting.
Let me put the code.
<?php
ob_start();
session_start();
if(isset($_POST['username'])){
$username = $_POST['username'];
$_SESSION['username'] = $username;
if(isset($_SERVER['HTTP_REFERER'])){
$referer = $_SERVER['HTTP_REFERER'];
header('location: '.$referer);
}
}
?>
<form action="page4.php" method="POST">
Username: <input type="text" name="username" /><br/>
<input type="submit" value="Submit" />
</form>
I am starting again all page1, page2, page3 with ob_start() and session_start();
If I use a specific page into the header function then it is redirecting, no problem
for example header (location: page2.php).
I am guessing the reason is maybe as my form field and the php script are at the same page (page4)
So how to redirect dynamically? User might come from page 1 or page2 or page 3 and after log in i want them back to the specific page they came from.
In page1.php, page2.php, page3.php:
<?php
session_start();
$_SESSION['page'] = $_SERVER['PHP_SELF'];
in page4.php:
<?php
session_start();
// process form
if ($form_proccessed == true) // or whatever
{
header("Location: {$_SESSION['page']}\r\n");
exit;
}
Using $_SERVER['PHP_SELF'] you wont have to worry about updating the code if you save the file as a new file with a new name.
It can be done with an hidden input field inside the formĀ :
<input type="hidden" name="referer" value="$_SERVER[HTTP_REFERER]">

Categories