Jump to content

$_SESSION throughout entire domain.


ConnorMcF

Recommended Posts

What I'm trying to do is get my $_SESSION to work throughout my website.

I'm quite new to PHP, so the PHP Manual didn't make much sense to me, so I thought I'd post here! ^^

 

I've got my login script under /session/ and I want it to be able to display your username on the homepage (/), but it only works inside /session/.

 

If you are wondering, I am using PHP-Login Advanced.

 

Thank you,

- Connor!

Edited by ConnorMcF
Link to comment
Share on other sites

Yeah like CroNiX said if you do not have session_start() and you try to use $_SESSION your going to get an error, and have you tried making a functions.php file and adding session expressions into it? because then you can just include the functions.php file into all the other .php files.

 

Also if you want to display the username do the following i'll add security in this example:

<?php

//Get values sent from form
$username = $_POST['username'];
$password = $_POST['password'];

//Strip tags from string
$username = strip_tags($username);
$password = strip_tags($password);

//Encrypt password
$password = hash('fnv164', $password);

//Escape from HTML
$username = mysqli_real_escape_string($username);
$password = mysqli_real_escape_string($password);

//Convert special characters
$username = htmlspecialchars($username, ENT_QUOTES);
$password = htmlspecialchars($password, ENT_QUOTES);

//If you are on another page and you want to echo the username
session_start(); //Critical this MUST be at the top of the page.

//Check if SESSION is set
if(isset($_SESSION[''])) {

$_SESSION['username'] = $username;

}

echo "Welcome, ".$username."!";

?>

Not sure if i went to over the top there but hopefully it should help you out :)

Edited by Tom8001
Link to comment
Share on other sites

What I'm trying to do is get my $_SESSION to work throughout my website.

I'm quite new to PHP, so the PHP Manual didn't make much sense to me, so I thought I'd post here! ^^

 

I've got my login script under /session/ and I want it to be able to display your username on the homepage (/), but it only works inside /session/.

 

If you are wondering, I am using PHP-Login Advanced.

 

Thank you,

- Connor!

 

Likely, the cookie associated with your cookie is only available to /session/ and other children directories.  Try using something like session_set_cookie_path() or session_save_path().

Link to comment
Share on other sites

As all previous posters above have stated make sure that you start your session

<?php
if(!session_id()){session_start();}
?>

 

EDIT: Actually you might not of installed PHP-Login Advanced correctly. As i have not used it myself of the installation options, you may want to try reinstalling that before you move on to my next suggestion. If there was anything about session cookies or directories you may want to keep them at default.

 

If that dose not work you php.ini may not be configured correctly i would edit that first or if you cant your Last option is.

 

<?php
session_set_cookie_params (0,'/','mywebsite.com',0,0);
if(!session_id()){session_start();}
?>

 

For more information on session_set_cookie_params

http://php.net/manual/en/function.session-set-cookie-params.php

 

--OffTopic

Dose anyone have a problem with $_SERVER['SERVER_NAME'] as a catch all ?

As it may be a better suggestion

session_set_cookie_params (0,'/',$_SERVER['SERVER_NAME'],0,0);
Edited by Werezwolf
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.