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