Sven van den Boogaart
Sven van den Boogaart

Reputation: 12327

Store current page in session

On my page i have a footer file wich i include in every page of my website. in the footer i want to save the current url to a session variable.

i have

$page = $_SERVER["REQUEST_URI"];
session_register("page");
echo "http://".$_SERVER['SERVER_NAME'].$_SESSION['page'];

but it only stores 1 value and dosnt change if i go to another page. i know there are other ways but i want is in session variable .

I hope someone can help me ?

i dont't know how to do it.

Upvotes: 0

Views: 6528

Answers (2)

Avro_Abir
Avro_Abir

Reputation: 166

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.

If we want the full URL of the page, then we'll need to check the protocol (or scheme name), whether it is https or http. See the example below:

<?php  
    if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')   
         $url = "https://";   
    else  
         $url = "http://";   
    // Append the host(domain name, ip) to the URL.   
    $url.= $_SERVER['HTTP_HOST'];   
    
    // Append the requested resource location to the URL   
    $url.= $_SERVER['REQUEST_URI'];    
    
    session_start();
    $_SESSION['page'] = $url;

    echo  $_SESSION['page'];  

  ?>   

Upvotes: 0

MarcinWolny
MarcinWolny

Reputation: 1645

$page = $_SERVER["REQUEST_URI"];
$_SESSION['page'] = $page;
echo "http://".$_SERVER['SERVER_NAME'].$_SESSION['page'];

Use of session_register is DEPRECATED.

Upvotes: 4

Related Questions