-1

i have got this code in my connect.php

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

And this in my Main.php

$_SESSION['firstname']=$firstname;
echo $firstname;

But it gives me this error Undefined variable: firstname in

J.doe
  • 205
  • 1
  • 2
  • 9
  • It's because in Main.php `$firstname` is undefined. – Daan Nov 18 '15 at 13:58
  • @Daan How can it be defined? – J.doe Nov 18 '15 at 14:02
  • Main.php is back to front: `$firstname = $_SESSION['firstname'];` – Steve Nov 18 '15 at 14:02
  • Though you will need to call `session_start()` in order to use sessions, as pointed out in the answer – Steve Nov 18 '15 at 14:03
  • Did you try setting a session variable directly in connect.php, eg `$_SESSION['firstname']='testingsessions';` to rule out an issue with POST? Also, you should have error reporting on, you would likely see your issues – Steve Nov 18 '15 at 14:11
  • @steve Yes i have done that and there was nothing on the screen – J.doe Nov 18 '15 at 14:14
  • No errors when you turned on error reporting?? – Steve Nov 18 '15 at 14:18
  • Have you tried `include 'path/to/file';`, that brings the variable into scope of the document (as long as you use the variable after the include) – Can O' Spam Nov 18 '15 at 14:19
  • @Steve Well now i have changed some things and it shows it.But i have deleted header so i cannot header user. – J.doe Nov 18 '15 at 14:20
  • What did you change? Care to add as an answer so the community can use this too? – Can O' Spam Nov 18 '15 at 14:20
  • @SamSwift Yes o have include connect.php – J.doe Nov 18 '15 at 14:21
  • Then surely all you need to do is submit the post and ten everything will be there? – Can O' Spam Nov 18 '15 at 14:22
  • `"But i have deleted header so i cannot header user"` Please clarify what this means, it doesnt make much sense – Steve Nov 18 '15 at 14:22
  • @SamSwift But i did not get what i want – J.doe Nov 18 '15 at 14:22
  • try `if ($_POST) { var_dump($_POST); } print '
    '; var_dump($GLOBALS);` at *the bottom* of your script or just before where the error is thrown and see what's held in them
    – Can O' Spam Nov 18 '15 at 14:23
  • @Steve I used header to redirect user from registration page.But now i have deleted that.I want user to be redirected from registration page and get all values of inputs that he filled. – J.doe Nov 18 '15 at 14:23
  • If you can, please also add the result from the `var_dump`s on your question to aid in getting an answer – Can O' Spam Nov 18 '15 at 14:24
  • It gave me long text.@SamSwift – J.doe Nov 18 '15 at 14:24
  • 1
    @J.doe This has become an odd guessing game. Please edit your question to include the actual code - it is pointless going backwards and forwards in comments like this – Steve Nov 18 '15 at 14:25
  • Please edit your question to include the *long text*, this may very well be the difference between an answer and a failed script for you :) – Can O' Spam Nov 18 '15 at 14:27

1 Answers1

0

In your connect you should have something like

<?php
session_start();

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

and in Main.php

<?php
session_start();

echo $_SESSION['firstname'];
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • I cant have session_starts in any of files it gives me this error session_start(); – J.doe Nov 18 '15 at 13:59
  • You must have it.. it must be called in each file where you want to use session variables. Please note that if you include the files you must open the session only in the parent file right after the php tag – Mihai Matei Nov 18 '15 at 14:00
  • yes i have opened it in parent so i don't open it again.I checked your code it is not giving me any errors but it is not showing anything. – J.doe Nov 18 '15 at 14:02
  • maybe because `$_POST['firstname']` is not holding anything? recheck your form input name to be sure it is correct – Mihai Matei Nov 18 '15 at 14:03
  • I header user from one page to main.php where there is input with name="firstname" so i guess it holds – J.doe Nov 18 '15 at 14:04
  • check if the form method is set to `POST` – Mihai Matei Nov 18 '15 at 14:05