1

I am trying to verify a session with a unique "key (in this case 303030330303)" from the first page and it needs to be verified by the second page using if.

Page one:

<?php
session_start();
$_SESSION['myValue'] = 303030330303;
?>

Then the second page, is supposed to take this variable (with the "key" 303030330303), match it against the "key" defined on the second page. If it matches, it will echo a success message if not else echo error message.

Page two:

session_start();
if(!empty($_SESSION['myValue'])) {

    if($_SESSION['myValue'] == 303030330303) {

        echo "Success";   

    }

}else { echo "Error"; }

This is what I have been fiddling with, but I cannot get it to work. Am a newb so pretty sure am doing lots of things incorrectly.

Any thoughts?

Thanks.

Nikk
  • 7,384
  • 8
  • 44
  • 90

2 Answers2

2

Look at the second if statement:

if($_SESSION['myValue'] == 303030330303;
                                     //^ Have to be a ')'

So just change it and use this:

if($_SESSION['myValue'] == 303030330303) {

Also you forgot one extra ')' in the first if statement:

if(!empty($_SESSION['myValue'])
                            //^ Here you forgot to close the if statement

So change it to this:

if(!empty($_SESSION['myValue'])) {
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • @Boris Yeah, sorry overlooked that you forgot to close the first if statement too, look at my edited answer. – Rizier123 Jan 22 '15 at 18:27
  • K, now it submits. However...I don't get the IF value, I get ELSE. – Nikk Jan 22 '15 at 18:28
  • @Boris Make sure you first called the first page and then you called the second one, it should work – Rizier123 Jan 22 '15 at 18:29
  • Yes, yay. It works. One more question, 303030330303 is viewable as HTML. Do I have an error on the fist page? – Nikk Jan 22 '15 at 18:31
  • @Boris No, you don't have a error you just simple print it with `echo`, if you look at this statement: `echo $_SESSION['myValue'] = 303030330303;` If you don't want to show it just remove the echo ;D – Rizier123 Jan 22 '15 at 18:34
  • Thanks... Doing so, I got other errors I didn't get before. " Warning: Unknown: write failed: No space left on device (28) in Unknown on line 0 Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct () in Unknown on line 0" – Nikk Jan 22 '15 at 18:36
  • @Boris This probably means that you ran out of space on your disk, so check that. (A related question here: http://stackoverflow.com/q/5412435/3933332) – Rizier123 Jan 22 '15 at 18:39
  • Thank you. Is this a reliable method for verification? – Nikk Jan 22 '15 at 18:41
  • Also, is it possible to delete the session on the second page? – Nikk Jan 22 '15 at 18:44
  • @Boris I would say it's pretty easy to do. So if you are on localhost you can just check there the disk and otherwise if you don't have access to this data on your host provider you can contact them – Rizier123 Jan 22 '15 at 18:44
  • @Boris For sure you can destroy your session when every you want. A good answer you can find here: http://stackoverflow.com/q/1226040/3933332 – Rizier123 Jan 22 '15 at 18:46
  • I added session_destroy(); at the end of the second page php. Do I also need to add it to the first page or just the second? – Nikk Jan 22 '15 at 18:49
  • @Boris Just in the second otherwise you wouldn't get any data on the second page. (Just try it out and you will see it :D) – Rizier123 Jan 22 '15 at 18:51
  • Did. Works! :) Thank you very much! Have an awesome day :D – Nikk Jan 22 '15 at 18:52
1
if(!empty($_SESSION['myValue']) {
                            //^ you miss a )
harrrrrrry
  • 13,643
  • 2
  • 23
  • 28