Jump to content

session


arnoldd99

Recommended Posts

Im having trouble with sessions. I want to create a variable that is avaliable for all pages of my site. The variable is determined by user input from a form. I have managed the first part:

 

$username=$_POST['username'];

 

I then wrote this for my session variable

 

<?

session_start();

$username=$_SESSION['username']=$_POST['username'];

?>

 

How do i use this variable on another page.

Link to comment
https://forums.phpfreaks.com/topic/77729-session/
Share on other sites

You'd use $_SESSION['username'] to access the username from the session.

 

Make sure you have session_start(); at the top of every page that uses sessions though in order for PHP to retrieve the current session variable.

 

I have done this but when i go to echo the variable to check the contents is there nothing appears.

 

Code i used:

 


$_SESSION['username'];
$username=$_SESSION['username'];
print ($username);

 

Link to comment
https://forums.phpfreaks.com/topic/77729-session/#findComment-393444
Share on other sites

sorry i wasnt very clear with my last post:

 

I have two pages, the first feeds.php

<?
session_start();
$username=$_SESSION['username']=$_POST['username'];
?>

<?php
$username=$_POST['username'];
?>

 

The second page userPage.php

 

<?php
session_start();
$_SESSION['username'];
$username=$_SESSION['username'];
print ($username);
?>

 

Is there still nothing assinged to the session variable?

Link to comment
https://forums.phpfreaks.com/topic/77729-session/#findComment-393451
Share on other sites

Still no luck.

 

Here is the code for the first page

 


<?php
session_start();
   $username=$_POST['username'];
   $_SESSION['username']=$username;	
?>
<html>
<head>
<?php
$username=$_POST['username'];
$data="$username \n";
</head>
  <body>
  <?php
$fh=fopen("$username.txt","a");
fwrite($fh,"$data");
fclose($fh);
print("Details sumbitted");
?>
<form action="userPage.php" method="POST" />
<input type="submit" value="click here to continue" />
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/77729-session/#findComment-393500
Share on other sites

Have you turned on error reporting in your php files, there must be some header errors in your previous code. Try this one...

 

<?php 
session_start(); // session start must be top of the page 
error_reporting(E_ALL); // this line added 
$username=$_POST['username'];
$_SESSION['username']=$username;	
// you dont have to open multiple php tags 1 is ok 
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$username=$_POST['username'];
$data="$username \n";
$fh=fopen("$username.txt","a");
fwrite($fh,"$data");
fclose($fh);
print("Details sumbitted");
}
?>
<html>
<head>
<form action="userPage.php" method="POST" />
<input type="submit" value="click here to continue" />
</form>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/77729-session/#findComment-393504
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.