How can i get the value of previous url in php code? - php

How can i get the value of previous url in php code?
Here is the url page1.php
http://localhost/spk/kelulusan_process.php?lulus=10003
page2.php:
I want to get the parameter from the previous url in page1.php. I use
<?php
$_GET['lulus'];
?>
But the value is null.

For $_GET to work you have to redirect user using form action
Or you can do that by setting session variables and access them any page

You can try $_SERVER["HTTP_REFERER"]
But don't forget to escape $_SERVER["HTTP_REFERER"] since it's common for attacks.
Better is to store the current page in a $_SESSION var.
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['lastpage'] = $_SERVER['REQUEST_URI'];
Then when loading the next page:
if (!isset($_SESSION)) {
session_start();
}
// now you can access the last page
$lastpage = $_SESSION['lastpage'];

page2.php
<?php
echo htmlspecialchars($_GET["lulus"]);
?>
how are you trying to pass from page 1?
if through a form set the method to get and action to page2.php
in page1.php make sure you set the name
<form method="get" action="page2.php">
<input type="text" name="lulus">
<input id="submit" name="submit" type="submit" value="Send">

You cannot display $_GET['lulus']; in page2.php unless you write get parameter in page2.php.
Your code is redirect from page1.php to page2.php, You must set $_GET['lulus']; into one session and echo in page2.php
Page1.php
<?
$_SESSION['GET'] = $_GET['lulus'];
?>
Page2.php
<?
echo $_SESSION['GET'];
?>
Instead, echo $_GET['lulus'], you should echo in page2.php $_SESSION. Because $_SESSION variable was overrode by $_GET itself.

Use $_SERVER['HTTP_REFERER'] ... hopefully this will help you. And parse url something like below:
$url = $_SERVER['HTTP_REFERER'] ; //http://localhost/spk/kelulusan_process.php?lulus=10003
$array = explode("?lulus=",$url);
echo $array[1];

Related

Can php session variable be changed, and if so how?

I am totally confused with session variables. I thought that if I set a session variable, then that variable will be available in any php document that begins with session_start. But it's not working.
Form:
<?php
session_start();
if(isset($_POST['hour'])) {
$_SESSION['hour'] = $_POST['hour'];
}
?>
<!DOCTYPE html>
<html>
<body>
<form action='viewA.php' method='post'>
<input type="text" name='hour' value='24'>
<input type ='submit' name= 'submit' value='submit'>
</form>
</body>
</html>
I post to viewA.php, and it works:
<?php
$hour = $_POST['hour'];
echo 'I am view A, and hour is '.$hour;
?>
<html>
<a href='View_B.php'>See View B</a>
<a href='TEST_form.php' >Choose another hour</a>
</html>
The file viewA.php has a link to View_B.php; here's View_B's code:
<?php
session_start();
print_r($_SESSION);
//$hour = $_SESSION['hour'];
//echo '... and in view B, hour is '.$hour;
?>
<html>
<a href='aatestform.php' >Choose another hour</a>
</html>
No matter what I put into the input in the form, the print_r($_SESSION); View_B outputs only Array ( [hour] => 13 ), which is the first hour I chose way back when. I type in "22"; it outputs 13. I type in "08", it outputs 13.
According to w3schools, "To change a session variable, just overwrite it"
What am I doing wrong? Please help!
In your viewA.php you don't store / overwrite the session variable with the $_POST value.
You just do it in your TEST_form.php which doesn't get any $_POST so your if(isset(... is useless.
Your post destination (the action) is viewA.php, this means that your request will be made to viewA.php.
You're using session variabiles only in the form page and in View_B.php.
If you look carefully at your code in ViewA.php, you'll see that you're working only with POST variables, not session variables.
This php code, that you have in your form page
<?php
session_start();
if(isset($_POST['hour'])) {
$_SESSION['hour'] = $_POST['hour'];
}
?>
Should be moved to viewA.php.
Doing this, viewA.php will check if the POST variable "hours" is set. In that case, it overwrite (or create) the session variable "hours".

Passing Variables Between Scripts in PHP

I have a simple question, with maybe not so simple of an answer. I want to be able to set a variable in one script and pass that value and variable to another script. Without passing it through the url, and having the ability to pass an array.
So I have index.php in there I have a variable
<?php
$myvariable = '';
<form action=editRecord.php>
do some form actions here and submit moving to editRecord.php
</form>
?>
Now in editRecord.php
<?php
header('Location: go back to index.php);
run functions and edit the Mysql DB
//Ok now here is where I want to assign a value to $myvariable and pass it back to index.php
?>
Is this possible? I am sorry for the amateurish question, but I am very green when it comes to php.
Thank You.
you can set it in the $_SESSION variable:
<?php
session_start();
$_SESSION["myVar"] = "blabla";
....
Of course you can store an array() in this variable too.
Just pass that information in the querystring. Then you can access it via $_GET. If it doesn't exist, just set the value to an empty string or whatever default value you want it to have.
// editRecord.php
<?php
// do db dtuff
header('Location: index.php?myvariable=somevalue');
?>
// index.php
<?php
$myvariable = (isset($_GET['myvariable'])) ? $_GET['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can also use session variables:
// editRecord.php
<?php
session_start();
// do db stuff
$_SESSION['myvariable'] = 'somevalue';
header('Location: index.php');
?>
// index.php
<?php
session_start();
$myvariable = (isset($_SESSION['myvariable'])) ? $_SESSION['myvariable'] : '';
<form action="editRecord.php" >
</form>
?>
You can use sessions, POST data, GET data, cookies and database data.

Possible to manipulate $_POST variable in php script and express it in another php script?

I've been trying to do form validation without using the url. So I thought that I would create a hidden field in my form and send it over to my validation php script. What I was hoping I would be able to do is set what ever errors there are in the form to this hidden field and return it. However once I get out of the scope it destroys whatever I set. I thought $_POST had global scope? Maybe I declared I set the hidden field wrong? I have placed the code below.
<?php
include_once $_SERVER['DOCUMENT_ROOT'].'/poles/config/databaseConnect.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/poles/config/functions.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/poles/models/users.php';
include_once $_SERVER['DOCUMENT_ROOT'].'/poles/models/userDetails.php';
//get the refering url to be used to redirect
$refUrl = $_SERVER['HTTP_REFERER'];
if(isset($_POST['register'])){
//declare a temp error array
$tempError;
//check if the form is empty
if(empty($_POST['Email'])&&empty($_POST['Email Confirmation'])&&empty($_POST['Password'])&&empty($_POST['Password Confirmation'])
&&empty($_POST['Stage Name'])&&empty($_POST['Main Club'])){
$tempError = 'Please fill in the form.';
}else{
//set variables
}
if(!empty($tempError)){
//start a session to declare session errors
$_POST['errors'] = $tempError;
//redirect back to referring url
header('Location:'.$refUrl);
exit();
}else{
//log user in and redirect to member home page
}
}
Basic form (I excluded the input field as it would be really long)
<div class="col-md-6 well">
<span class="jsError"></span><?php if(isset($_POST['errors'])){ $errors = $_POST['errors']; } if(!empty($errors)){ echo '<p class="alert alert-danger text-center">'.$errors.'</p>'; } ?>
<form class="form-horizontal" role="form" method="post" action="controllers/registrationController.php" id="registration">
<input type="hidden" name="errors" value="<?php if(isset($_POST['errors'])){echo $_POST['errors']; } ?>">
</form>
I looked into using the $_SESSION variable method too but the stuff I found was either a bit complicated or it involved me starting a whole bunch of sessions everywhere (would make my code messy in my opinion).
$_POST is populated from the contents of the data passed by the browser to the server. When you send a Location header it causes the browser to load a new page, but since it will have no form data, nothing will be passed.
If you need to pass data from page to page then $_SESSION is the way to go. All that is required is a session_start() at the top of the pages that need access, and you can store your $_POST data like this:
$_SESSION['postdata'] = $_POST;
Retrieving it becomes
$email = $_SESSION['post']['Email'];
The alternative is to echo the data as a hidden <input> in a new form, but that will require a new form to be submitted and I get the feeling you want something seamless.
Note also that $_SERVER['HTTP_REFERER'] is not guaranteed to be accurate, or even present. You shouldn't rely on this for production code. It might work for you with your browser in your test set-up, but that's no guarantee it'll work for other browsers. Find another way.
You can achieve this by using javascript instead of a redirect, but the only way to pass data through a redirect is via the URL, the session, or cookies.
$_POST['errors'] = $tempError;
//redirect back to referring url
?>
<html><head><title></title></head><body>
<form id="temp_form">
<?php
foreach($_POST as $k=>$v) {
?><input type="hidden" name="<?php echo htmlentities($k); ?>" value="<?php echo htmlentities($v); ?>" /><?php
}
?>
</form>
<script type="text/javascript">
setTimeout(function() { document.getElementById('temp_form').submit(); },100);
</script>
</body>
</html>
<?php
die();

session gets lost after form submit

When a user links to link it redirects to edit.php - here's an example: www.cars.com/edit.php?id=23
In edit.php, I use _GET to store the value in a session. The value is stored in $_session['user'] but when the form on the same page is submitted echo $_session['user'] displays nothing - how can I make it display the value?.
<?php
session_start();
$_session['user']=$_GET['id']; // I use _GET to store the value in session
if( isset($_POST['submit'])) {
echo $_session['user'];
}
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" name="formI2D" enctype="multipart/form-data" id="formI2D" />
It's because you're redeclaring your $_SESSION['user'] even when it's a POST (I think).
You can fix this by adding ?id=$_GET['id'] in you form's action, or by wrapping your $_SESSION initialisation like that:
if (isset($_GET['id'])) {
$_SESSION['user']=$_GET['id'];
}
Also, you should use uppercase for php global arrays ($_POST, $_COOKIE, $_SESSION etc)

Including form data in another page

When I post my form data:
<form action="layouts.php" method="post">
<input type="text" name="bgcolor">
<input type="submit" value="Submit" align="middle">
</form>
to a php page, "layouts.php", it returns the string, as expected.:
$bgcolor = $_POST['bgcolor'];
echo $bgcolor; //returns "red"
echo gettype($bgcolor); // returns "string"
But when I include "layouts.php" in another page it returns NULL.
<?php
include("php/layouts.php");
echo $bgcolor; //
echo gettype($bgcolor); //returns "NULL"
?>
How do I pass the variable to another page?
You'll have to use a session to have variables float around in between files like that.
It's quite simple to setup. In the beginning of each PHP file, you add this code to begin a session:
session_start();
You can store variables inside of a session like this:
$_SESSION['foo'] = 'bar';
And you can reference them across pages (make sure you run session_start() on all of the pages which will use sessions).
layouts.php
<?php
session_start();
$bgcolor = $_POST['bgcolor'];
$_SESSION['bgcolor'] = $bgcolor;
?>
new.php
<?php
session_start();
echo $_SESSION['bgcolor'];
?>
Give the form two action="" and see what happens. I just tried that on a script of mine and it worked fine.
A more proper way to solve this might exist, but that is an option.

Categories