Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Help with php sessions

  1. #1
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with php sessions

    hi,
    i am creating a site in which a login page has to be created using sessions and after logging in we have to check using session session if user is logged in and only then give him access to the pages in the site.
    also i require the username in each page.how do i get it through session?
    please help.
    thanks in advance..
    Suk

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Include in every page this:
    <?php session_start(); ?>

    Then you can get and store values in $_SESSION like this:
    $var = $_SESSION['var'];
    And:
    $_SESSION['var'] = $var;

    You can access those values for the length of the session on any page with session_start() at the top.

    You MUST put session_start() before ANY text output, or it will cause an error.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    Moshambi (08-28-2008)

  4. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Code please?
    - Josh

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If you need a full, working code, that takes a long time to write. Twey has a script around (do a search) which works without much setup. There are other scripts, too.
    All I explained above was how to use Session variables when you need them.
    A login is just a complex setup using session and forms, and checking if the session is verified.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    hehe, i wasn't asking you for code, i was asking the original poster of this thread for some starting code for me to help ;-)
    - Josh

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Oh, I misunderstood. You're right, though I gave a general answer because code wasn't posted. sukanya may just want some ideas of where to start.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    thanks for the replies,
    my query is that i have used session_register('$var'); but i am not able to retrieve the value when i am going to other pages.Also when i am checking if(session_is_registered('user')) the answer is always true.

    admin_login.php
    Code:
      <div id="main2">
        <div>
          <h2>Admin Login</h2>
    		<p>&nbsp;</p>
    
    
    <form action="log_code.php" method="POST">
    <table width="100%" border="1" cellpadding="10" cellspacing="5	" bordercolor="#E0DFE3" >
    			<tr> 
    				<td width="23%" id="t_uname">Username:&nbsp;&nbsp;</td>
    			  <td width="77%"><input name="username" type="text" size="20" /></td>
    			</tr>
    			<tr> 
    				<td id="t_pword">Password:&nbsp;&nbsp;</td>
    				<td><input name="password" type="password" size="20" /></td>
    			</tr>
    			<tr> 
    				<td >&nbsp;&nbsp;</td>
    				<td><input type="submit" value="Submit" name="login"> &nbsp;<a href="changepw.php">Change password?</a></td>
    			</tr>
    </table>
    
    </form>
    
    
     		<p>&nbsp;</p>
      </div>
      </div>
    log_code.php
    Code:
    <?PHP
    //check that the user is calling the page from the login form and not accessing it directly
    //and redirect back to the login form if necessary
    //convert the field values to simple variables
    
    //add slashes to the username and md5() the password
    $user = addslashes($_POST['username']);
    $pass = $_POST['password'];
    
    
    $con = mysql_connect("localhost", "root", "");
    if(!$con)
      {
        die("could not connect" . mysql_error());
      }
    mysql_select_db("db_dayak", $con);
    
    $result=mysql_query("select * from registration where email='$user' AND password='$pass'");
    
    //check that at least one row was returned
    
    $rowCheck = mysql_num_rows($result);
    if($rowCheck > 0)
    	{
    		while($row = mysql_fetch_array($result))
    			{			
    			  //start the session and register a variable			
    			  session_start();
    			  session_register('user');
    			  $type=$row['type'];
    			  $user=$row['email'];
    			  $fn=$row['first_name'];
    			  $ln=$row['last_name'];
    			  $name= $fn.$ln;
    			  
    			  if ($type=='employer')
    				   {
    					  include('employer.php');
    				   }
    			  else if ($type=='recruiter')
    				   {
    					  include('recruiter.php');
    				   }
    			  
    			 }
    	
    	}
      else {
    
      //if nothing is returned by the query, unsuccessful login code goes here...
    
      echo 'Incorrect login name or password. Please try again.';
      }
     
      ?>
    employer.php
    Code:
    <?php
    
    //start the session
    session_start();
    
    //check to make sure the session variable is registered
    if(session_is_registered('user')){
    
    //the session variable is registered, the user is allowed to see anything that follows
    $username=$user;
    echo $username;
    
    ?>
    
    <html>
    <body>
    Some code
    </body>
    </html>
    <?php
    }
    else{
    
    //the session variable isn't registered, send them back to the login page
    echo "You have been logged out.Please login.";
    }
    
    ?>
    From employer.php or recruiter.php i have many subpages where i need to check is the user is logged in and also the username for retrieving mysql values.But i am not able to.

    Please help if possible.
    Thanks..Suk

  9. #8
    Join Date
    Mar 2007
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Anyone???any hint??

  10. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't see why you'd be using those functions. As I posted above, just using $_SESSION['variable'] is fine. So you can use $_SESSION['a'] = 'value';, or you can check if it is set like: if (isset($_SESSION['a'])).
    Seems easier to me.
    However, if you do want to use them, I'd just say copy an example from php.net to see how they work.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. #10
    Join Date
    Aug 2008
    Location
    Worldwide
    Posts
    11
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    session_register() is depreciated, since php 4.3 I think it was. You should use the super global $_SESSION[]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •