-7

There are my both codes where error is,where the session variable is not executed in session2.php page and these are the errors:

Notice: Undefined index: favcolor in C:\xampp\htdocs\php\vehiclebazar\session2.php on line 10 Favorite color is .
Notice: Undefined index: favanimal in C:\xampp\htdocs\php\vehiclebazar\session2.php on line 11 Favorite animal is .

session1.php:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html> //html type
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";
echo "Session variables are set.";
?>

</body>
</html>

2nd page is

session2.php:

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
?>

</body>
</html>
15
  • 3
    Why in single quotes? '$' just remove them! Also The comment style: // only works in php tags in html use: <!-- ... --> Commented Jan 3, 2015 at 19:44
  • kindly answer this question Commented Jan 3, 2015 at 19:44
  • 1
    You really should stop with the STEALTH edits. That means, overwriting your question with codes without marking them as edits. My answers works; period. Commented Jan 3, 2015 at 20:05
  • finaly it is orginal code Commented Jan 3, 2015 at 20:05
  • 1
    Nouman, aside from Fred's advice about not substantively changing a question while it is being answered, it would also be good if you would stick to informative titles. Try to keep these succinct and free from chat (no "I am a beginner") as that's not of use to other people who may learn from this question in the future. I've edited it for you now. Commented Jan 3, 2015 at 20:43

3 Answers 3

6

This, as per your initial/original post and not marking it as an edit, including all other edits you've made.

which your edit still contains a quote $'_SESSION["favcolor"] between $ and _


You see these '$'_SESSION they can't contain quoted dollar signs.

All of those need to read as $_SESSION in one go, which is why you're getting those warnings.

Read up on this superglobal

These superglobal variables are:

$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV

Also, in your first file, you have

<?php//opening of php

that alone will throw an error, had you error reporting set. Either remove the //opening of php or put it in a second line:

<?php 
//opening of php

The error in question:

Parse error: syntax error, unexpected '$_SESSION' (T_VARIABLE)...


While testing your code and after fixing all of those errors, did in fact post

Favorite color is green.
Favorite animal is cat.

in the second file.

You should make it a habit not to have comments so close to executable code, especially the opening <?php tag.

Another being session_start();//where session start it won't throw/cause an error, but it's just confusing.


You could also check if both sessions are set:

if(isset($_SESSION["favcolor"]) && isset($_SESSION["favanimal"])){
    echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
    echo "Favorite animal is " . $_SESSION["favanimal"] . ".";
}

else{
    echo "Sessions are not set.";
}

Plus, since you said you were doing this via localhost, make sure that the sessions/tmp folder(s) are writeable and have proper write permissions set.

Use:

<?php 
phpinfo();

in a seperate file to see what your sessions settings are set to.


Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Error reporting should only be done in staging, and never production.

Sign up to request clarification or add additional context in comments.

6 Comments

Or perhaps read up on variables in php in general.
lol he just updated the question and now it's only 1 quote he has too much
@Rizier123 You gotta love those stealth edits lol
Phew, you have far too much patience, Fred! Great effort, well done.
@halfer Lord as my witness, knows I try. Thanks. Oh, and I grow "cactus" lol so I do have a lot of patience.
|
0

You should use $_SESSION["favcolor"] not '$'_SESSION["favcolor"]

http://php.net/manual/en/language.variables.superglobals.php

2 Comments

i correct them but still warrning . .
if your code is right up there still have $'_SESSION["favcolor"]. You should change $_SESSION["favcolor"]
0
'$'_SESSION["favcolor"] = "green";
'$'_SESSION["favanimal"] = "cat";
echo "Favorite color is " . '$'_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . '$'_SESSION["favanimal"] . ".";

In PHP, a variable should look like $var, not '$'var. So instead of '$'_SESSION["favcolor"] and '$'_SESSION["favanimal"] use $_SESSION["favcolor"] and $_SESSION["favanimal"].

Further reading on PHP variables: http://php.net/manual/en/language.variables.basics.php

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.