0

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.

3
  • try echo $_GET['lulus']; Commented Mar 26, 2015 at 4:51
  • i have try it.but the value still null. Commented Mar 26, 2015 at 4:52
  • 1
    I guess you should use session variables. Commented Mar 26, 2015 at 4:56

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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">

2 Comments

$link = $_GET["lulus"]; echo "<a href="page2.php?" . $link . "\">page 2</a>"; or page1.php $_SESSION['GET'] = $_GET['lulus']; page2.php echo $_SESSION['GET'];
thanks. i have try it but the value still null. i want to get the value of lulus "10003".
0

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.

2 Comments

look at my newest answer
thanks. i got it. i forget to put session_start() in page1.php
0

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

3 Comments

i have try it. but it show the whole url. i just want the value of 10003. by the way, thanks.
You need to parse url
You are welcome..If you think my answer help you to solve your problem so please don't forget to accept answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.