Jump to content

[SOLVED] Little help on sessions :P


harsh00008

Recommended Posts

Hey  ;D

 

I after logging in in my login page i want a session to start so that it caries value through all the pages...

 

that means if the session is set then only someone can view a topic else he'll need to login  :P

 

any suggestions?  ::)

 

I tried the session thing but it doesn't carry value to the other page  >:(:(

 

any simple explanation/help would be great  :D

Link to comment
Share on other sites

You need session_start() on the top of every page that requires session use.

 

That means wen i login..

 

for eg in my case...wen i input the login variables and hit the login button i get directed to the login.php page where it checks for the inputs and echoes that u r logged in..

 

 

so at that time i shall put session_start();

$_SESSION["logged"]=1 in the login.php if the account is verfied

and on all restricted page

session_start();

if($_SESSION==1)

{

blah blah codes

}

Else

{

Echo "you need to login to view this page";

};

 

???

 

 

and then session_start() on every page where i want the permission?? ???

 

and on logout.php put the session_destroy() code? :-?

Link to comment
Share on other sites

well that code you kinda sorta posted you did

 

if($_SESSION==1)

 

it's supposed to be

 

if($_SESSION["logged"]==1)

 

I just assumed you were showing some pseudo-code to get your point across but if that's what you really did..

 

yeah..forgot to type["logged"] there

 

but in the codes its ["logged"]

 

any reason or method to carry the $_SESSION["logged"] value to all the pages?  ???

Link to comment
Share on other sites

It's really simple:

 

page1.php

<?php
   // must have this before any output and before you want to use session vars
   // must even be on the page where the session var is first set
   session_start();

   // example session var
   $_SESSION['logged'] = 1;

   // example to get to next page. can use header() or whatever who cares
   echo "<a href = 'page2.php'>page2</a>";
?>

 

page2.php

<?php
   session_start();

   // is there a session var called 'logged' and does it equal 1?
   if ($_SESSION['logged'] == 1) {
      echo "yay!";
   } else {
      echo "nay!";
   }
?>

 

If that's the basic format you have and you're sure everything is spelled right etc.. then perhaps you have cookies turned off in your browser?

Link to comment
Share on other sites

It Echoes "nay" in both the cases >:(

 

even after u goto page2 via the a href function ,i.e., via page1>:( :(

 

I'm testing this project on my localhost

 

is it something related to this...that the browser cookies or the session function don't always work well on a local server  ???

Link to comment
Share on other sites

As far as i can tell they are. Show the two pages code where the problem is

 

Hey buddy I tested these simple session codes to test if something was wrong in my codes

 

Where page1.php is

 

<?php

 

session_start();

$_SESSION["logged"]=1;

Echo "<a href=page2.php>Page2</a>";

 

?>

 

and page2.php is

 

<?php

 

session_start();

if($_SESSION["logged"]==1)

{

Echo "working";

}

Else

{

Echo "not working";

};

?>

 

 

 

and wen i access the page2 via the page1 link it still echoes not working!!! ;D

Link to comment
Share on other sites

My guess is that it can't write the session files to the folder session.save_path

Is PHP installed into C:\php? If so, there should be a sessions folder in there

If you change the session save path in the php.ini to the C:\php\sessions

 

If you are using IIS as the web server you will also need to add the IUSR user to have permissions on that directory

Link to comment
Share on other sites

so...does that folder have appropriate permissions?

 

just for shits and grins, try this:

 

<?php
session_start();
echo session_id();
?>

 

does something echo?

 

yeah it echoes a hash like some md5 encryption

 

62467318e5d1474cea48c7274fb090af

 

Well i think if some session files are present there then i guess it must be having proper permissions :o

 

but even if so...how do i change the permission on'em?..like for the web hosting i CHMOD them via ftp manager..

 

I'm really confused y this thing isn't working :(

 

 

 

Link to comment
Share on other sites

Get php to help by turning on full php error reporting. Add the following two lines immediately after the first opening <?php tag on both pages -

 

ini_set ("display_errors", "1");
error_reporting(E_ALL);

:o

i got these three error lines on page1.php :o

 

Warning: session_start(): open(C:\DOCUME~1\Harsh\LOCALS~1\Temp\php\session\sess_62467318e5d1474cea48c7274fb090af, O_RDWR) failed: No such file or directory (2) in c:\program files\easyphp1-8\www\page1.php on line 6

 

Warning: Unknown(): open(C:\DOCUME~1\Harsh\LOCALS~1\Temp\php\session\sess_62467318e5d1474cea48c7274fb090af, O_RDWR) failed: No such file or directory (2) 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 (C:\DOCUME~1\Harsh\LOCALS~1\Temp\php\session) in Unknown on line 0

 

AND

 

these error line on page2.php

 

 

Warning: session_start(): open(C:\DOCUME~1\Harsh\LOCALS~1\Temp\php\session\sess_62467318e5d1474cea48c7274fb090af, O_RDWR) failed: No such file or directory (2) in c:\program files\easyphp1-8\www\page2.php on line 4

 

Notice: Undefined index: logged in c:\program files\easyphp1-8\www\page2.php on line 8

nay!

Warning: Unknown(): open(C:\DOCUME~1\Harsh\LOCALS~1\Temp\php\session\sess_62467318e5d1474cea48c7274fb090af, O_RDWR) failed: No such file or directory (2) 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 (C:\DOCUME~1\Harsh\LOCALS~1\Temp\php\session) in Unknown on line 0

 

:o;D

 

 

Link to comment
Share on other sites

Much better, as I first thought you need to change the directory it saves session files to

Instead of /tmp try C:/php/sessions

 

u mean i change this line ;session.save_path = /tmp

 

to

 

;session.save_path = C:/php/session

 

???

 

but i don't have a folder existing as c:\php

 

I have easyphp installed for the localhost and the path of php is

 

C:\Program Files\EasyPHP1-8\php

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.