How to make website users go through pages in order - php

I am working on a simple website that requires users to enter information in order over 3 different pages. So they need to go from page A to B to C. How can I stop them from typing in www.example.com/pageB.php, which skips page A?
Also, I'm not even sure what you call pages that must be visited in order, so any suggestions for google search terms on this also appreciated.

You can store vars in session to check whether the user has visited previous pages.
page1.php
<?php
$_SESSION['has_visited_page1'] = true;
....
page2.php
<?php
if (empty($_SESSION['has_visited_page1'])) {
exit('You must visit page 1 first');
}
If it's a form, you can store in session the field values from previous page.

you can check page refer info if the page refer not your expected then redirect it or show something you wanted
edited1
in PHP, you can try the code in your pages
var_dump(#$_SERVER["HTTP_REFERER"])
you'll know how to write it.

Related

How can I change the default html / php landings page to another one if I clicked on a link

I want to change the landings page if i click on a button or link
So for example I have two pages: page-1 and page-2.
If i click on link-2 the default landings page get changed from page-1 to page-2 and if I click on link-1 the default landings page get changed from page-2 to page-1.
If i leave the website and come back i want that the default landings page is still the same as it was set last time.
This can be easily done by setting a cookie like this:
<?php
if($_GET['landingpage']) setcookie("landingpage", $_GET['landingpage'], time()+3600);
if($_COOKIE["landingpage"] == 1) {
echo "landingpage 1";
}
if($_COOKIE["landingpage"] == 2) {
echo "landingpage 2";
}
if(!$_COOKIE["landingpage"]) echo "default page";
?>
landingpage1
landingpage2
Check https://www.php.net/manual/en/function.setcookie.php for more info on expiration, etc.
Also beware: Not all users allow setting cookies.
If you have a PHP server that can store data (I'm assuming this because of the PHP tag), then you would handle it like this:
When the user hits your landing page, pull their landing page preference from your PHP data store and use that to determine what page they hit.
If the user clicks your link, you need to send that information back to the PHP server so that it can store their preferences, likely via an API call.
This is very high level, because both of these tasks tend to have a lot of different ways. I'd recommend trying to make this work, then asking specific questions if you get stuck.

Website redirect based query with cookie

I have a website that used to be only a news page, but as we grown, I had to move the news section from homepage to domainname.com/news
To not confuse our loyal users when they landing on the new home page, I would like to give them an option (light box). The first time when you arrive to the homepage, you get a message, if you want to go directly to News and keep it as a default (or cancel). If you choose yes, next time you type our domain name, it will take you to the news section.
I hope there is a javascript that could do this job.
Use JavaScript to do an alert confirm() to find out what the user wants to do. From that set a cookie.
Next load, have PHP check the cookie to find out where to head to.
function checkCookie()
{
var wellcome=getCookie("firstime");
if (wellcome!==null && wellcome!=="")
{
alert("Welcome");
}
else
{
alert("is your first time");
setCookie("firstime",true,365);
}
}

Limiting Access to a Specific Page By Requiring Sequential Order of Page Views

I am trying to limit page access so that the user must enter the page in question by clicking a button on 1 of 3 pages.
So, to be clear, page x can only be accessed if the user clicks a button from page 1, 2, or 3.
I've tried using isset if/else blocks, but cannot seem to make any progress with limiting the page access in this way. If anyone has any suggestions I'd be very grateful!
use sessions ...
for example
session_start();
$_SESSION=array();
in each page add a value to the array
$_SESSION['page1']="viewed";
$_SESSION['page2']="viewed";
In your secured page check the elements of the $_SESSION array
Set a cookie when said button is clicked, and check for that cookie when that page is visited.

PHP passing URL parameters best practice

I have a search page that outputs search results. You can filter results based on on the following criteria before running the search.
1. status
2. order by
3. record type
Viewing a record redirects the user to the edit form page and if they click the cancel button they redirect back to the search results.
Here's what I've done:
I stored the GET parameters (status,order_by and record_type) in session variables
When the user clicks cancel on the edit form, I use the session variables to redirect back to the search results as follows:
if (#$_POST['cancelbtn']){
if (isset($_SESSION['searchForm'])){
header("location:searchForm.php?products=".$_SESSION['product']."&ticket_status=".$_SESSION['status']."&order_by=".$_SESSION['order_by']."&record_type=".$_SESSION['record_type']."&searchbtn=Go!");
}
}
The above works for me but I'd like to know if this is the best way to do it? Thanks.
It's not a bad way of doing it, but I would try and future proof it a bit. Instead of storing each individual variable in the session, try storing the URL. That way, if you change your search form to have some additional parameters, you won't need to change much of your code.
//Your search results page
$_SESSION['searchResults'] = $_SERVER['REQUEST_URI'];
//Your form page
if (array_key_exists('cancelButton', $_POST) {
header('Location: '.$_SESSION['searchResults']);
exit;
}
Hope this helps

let jquery know which was the last visited php page?

this is a little bit tricky.
usually when someone clicks on a link that requires him to register, this person will be redirected to the registration page and then back to the last visited page. this is possible cause the link sent a GET key through the url to the registration.php which uses the key to go back to last visited page.
but i intend to use jquery ajax for registration. basically ive got 3 different php pages. all of them include the same header.php. and in header.php ive got a registration button which i have id tagged. when this button is clicked ( $(#registration_button).click()... ) jquery will show a box (a div that was hidden in the center of the browser) with registration information. then he will register and i will redirect him to the last visited page, that is to say the current one he sees. i have to refresh the php-page to be able to show all links that a registered user can see, thats why i have to use window.location.href.
now to the question. how do i let jquery know which page is the current one he is visiting? ive got 3 php-pages.
if there is something you dont understand, please free to ask.
or if you got suggestions of other solutions, let me know. but i really want to display the registration box right away without redirecting him to another page.
You can set cookies initially in php and then update/read them via js.
You could assign the page to a session and do it that way.
$_SESSION["page_visited"] = "x.php";
Make sure to use session_start on the pages using sessions. Then just redirect to the relevant page.
header('Location: http://www.example.com/'$_SESSION["page_visited"]);
I did a similar thing not two weeks ago, correct me if I'm wrong, but if you want the registration to direct to the page the user was on, after the user has been registered in the ajax just add:
window.location.href=window.location.href;
That way the after the registration is done, it just reloads where the member was with the environment of a logged in user. This method worked great for me.

Categories