Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 27

Thread: session>> need help!

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

    Default

    OK, we don't use session_register(); in modern versions of php anymore. Additionally, you need to start the session on each page you want to protect, and use $_SESSION to register vars.

    PHP Code:
    <?php 
    $host 
    "server"
    $username "asasas"
    $password "tututu"
    $db_name "phpacademy"
    $tbl_name "members"

    mysql_connect($host$username$password) or die(mysql_error("can't connect")); 
    mysql_select_db($db_name) or die(mysql_error());  

    $myusername $_POST['myusername']; 
    $mypassword $_POST['mypassword']; 

    $sql "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'"
    $result mysql_query($sql); 

    $count mysql_num_rows($result); 

    if(
    $count==1) { 

     
    session_start();

     
    $_SESSION["myusername"];
     
    $_SESSION["mypassword"];

     
    $_SESSION[$myusername];
     
    $_SESSION[$mypassword]; // you need to declare what sessions are storing also

     
    $message "Success!"
     } 
     else { 
      
    $message "Wrong Username or Password"
      } 
    ?> 
    <html> 
    <head> 
     <title>login</title> 
    </head> 
    <body> 
    message: 
    <?php echo $message?> 
    </body> 
    </html>
    PHP Code:
    <?php

    session_start
    ();

    if (isset(
    $_SESSION['myusername'])){

    } else {
    header("Location: login.php"); // you can chg this to the login pg uri

    ?>

    ?>
    <html> 
    <head> 
    <title>Forum</title> 

    <!----------------------------------------------------------------> 
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
    <link rel="stylesheet" type="text/css" href="drilldownmenu.css" /> 
    <script type="text/javascript" src="drilldownmenu.js"> 
    </script> 
    <script type="text/javascript"> 
    var mymenu=new drilldownmenu({ 
        menuid: 'drillmenu1', 
        breadcrumbid: 'drillcrumb', 
        persist: {enable: true, overrideselectedul: true} 
    }) 
    </script> 
    <!----------------------------------------------------------------> 

    </head> 
    <body bgcolor="black" text='#000000' link='black' atext='#000000' vlink='black'> 
    <table width='500px' height='40px'> 
    <tr> 
    <td> 
    </td> 
    </tr> 
    </table> 
    <table background="images/general.JPG" align='center' width='765px' height='440px' style='border-color':black;border-style:solid;borderw-width:0px border='0'> 
    <tr> 
    <td> 
    <table align='left' width='200px' height='300px' style='border-color':black;border-style:solid;borderw-width:0px border='0'> 
    <tr> 
    <td> 
    <table> 
    <tr> 
    <td> 
    </td> 
    </tr> 
    </table> 
    <table width='150px' height='200px' align='left'> 
    <tr> 
    <td> 
    <!-----------------------MENU---------------------------------------> 
    <div id="drillmenu1" class="drillmenu"> 
    <ul> 
    <li><a href="">Minas Tirith</a></li> 
    <li><a href="members.php">Guild members</a></li> 
    <li><a href="forum.php">Forum</a></li> 
    <li><a href="killlist.php">Kill list  [Pk]</a></li> 
    <li><a href="#">Information</a> 
      <ul> 
      <li><a href="#">Empty...</a></li> 
      <li><a href="#">Empty...</a></li> 
      <li><a href="#">Empty...</a> 
      </li> 
      </ul> 
    </li> 
    <li><a href="#">Other guilds</a> 
      <ul> 
      <li><a href="enemies.php">Enemies</a></li> 
      <li><a href="allies.php">Allies</a></li> 
      </ul> 
    </li> 
    <li><a href="traitors.php">!!!TRAITORS!!!</a></li> 
    <li><a href="#">Shop</a> 
      <ul> 
      <li><a href="buy.php">Buy</a></li> 
      <li><a href="sell.php">Sell</a></li> 
      <li><a href="rps.php">RPS shop</a> 
        <ul> 
        <li><a href="https://shop.playrohan.com/ItemMall/ItemMall.html">Item mall</a></li> 
        <li><a href="https://shop.playrohan.com/Exchange/Exchange.html">Exchange</a></li> 
        </ul> 
      </li> 
      </ul> 
    </li> 
    <li><a href="http://www.playrohan.com">Play Rohan</a></li> 
    <li><a href="choosen.php">Choosen RooM</a></li> 
    </ul> 
    <br style="clear: left" /> 
    </div> 
    <!-------------------------MENU-------------------------------------> 
    <br><br><br> 
    </td> 
    </tr> 
    </table> 
    </td> 
    </tr> 
    </table> 
    <table align='right' width='500px' height='50px' style='border-color':black;border-style:solid;borderw-width:0px border='0'> 
    <tr> 
    <td> 
    <p><font size="5" color='#ECE5B6' face="Old English Text MT"><b>Minas Tirith guild site</b></font></p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> 
    </td> 
    </tr> 
    </table> 
    <!-------------------------langas-------------------------------------> 
    <table background="images/members.jpg" align='right' width='500px' height='360px' style='border-color':black;border-style:solid;borderw-width:0px border='1'> 
    <tr align='center'> 
    <td align='center'> 
    <p><font size="2" color='black' face="Verdana"><b>Sorry... Can't connect</b></font></p><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> 
    </td> 
    </tr> 
    </table> 
    <!-------------------------langas-------------------------------------> 
    </td> 
    </tr> 
    </table> 
    <table align='center' width='500px' height='120px' style='border-color':black;border-style:solid;borderw-width:0px border='0'> 
    <tr> 
    <td align='center'> 
    </td> 
    </tr> 
    </table>
    [PHP]
    - Josh

  2. The Following User Says Thank You to JShor For This Useful Post:

    auriaks (08-08-2009)

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

    Default

    Register page

    PHP Code:
    <?php

    session_start
    ();

    if(isset(
    $_SESSION["myusername"])) {
    header("Location: index.php"); // if is already reg/logged in
    }

    session_destroy();

    if(isset(
    $_POST[submit])) {

    $user mysql_query("SELECT * FROM users WHERE username='$_POST[username]'") or die(mysql_error());

    if(
    $_POST[password] == $_POST[confirm]) {

    if(
    mysql_num_rows$user ) > 0) {

    echo 
    "This user already exists";

    } else {

    mysql_query("INSERT INTO users (username, password) VALUES('$_POST[username]', '$_POST[password]') ") or die(mysql_error());

    echo 
    "You've successfully registered!";

    }

    } else {
    echo 
    "Your passwords don't match";
    }



    }

    ?>
    <html>
    <head>
    <title>Registration</title>
    </head>
    <body>

    <form action="<?php echo $_SERVER[PHP_SELF]; ?>" method="post">
    <b>Username:</b>
    <input type="text" name="username" value="<?php echo $_POST['username']; ?>"><br>
    <b>Password:</b>
    <input type="password" name="password"><br>
    <b>Confirm:</b>
    <input type="password" name="confirm"><br>
    <p>
    <input type="submit" name="submit" value=" Register ">
    <input type="reset" value=" Reset ">

    </form>
    </body>
    </html>
    I don'tknow what your registration fields, so I only assumed username and password,but the other fields are easy to implement.

    HTH
    - Josh

  4. The Following User Says Thank You to JShor For This Useful Post:

    auriaks (08-08-2009)

  5. #13
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

    This is the error of register... what i have to change to mine MySQL information?

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

    Default

    Right, sorry, you need to call the db to connect to it first. Insert this into the beginning of your registration PHp code:

    PHP Code:
    <?php 

    $host 
    "server"
    $username "asasas"
    $password "tututu"
    $db_name "phpacademy"
    $tbl_name "members"

    mysql_connect($host$username$password) or die(mysql_error("can't connect")); 
    mysql_select_db($db_name) or die(mysql_error());  

    ?>
    - Josh

  7. The Following User Says Thank You to JShor For This Useful Post:

    auriaks (08-10-2009)

  8. #15
    Join Date
    Jul 2009
    Location
    Coquitlam BC Canada
    Posts
    46
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Red face

    ok, i think i can help you with that.
    i'm also going to use some scripts from my login system.
    ok:
    login page
    Code:
    <?php session_start();?>
    <html>
    <head><title>Login</title></head>
    <body>
    <!--Login form-->
    
    
    <form action="process.php" method="get">
    	<h2>Login</h2>
    	<label>Username: </label><br />
    	<input name="user_name" type="text" />
    	<label>Password: </label><br />
    	<input name="password" type="password"  />
    	<input type="submit" value="Log In!">
    
    </form>
    </body></html>
    thats it for login.php
    ___________________
    create a member database with these fields:
    loginName , varchar(50)
    pass, varchar(255)
    createDate, datetime
    _________________
    call the database "member" without the quotes.

    heres process.php
    Code:
    <?php
    session_start();
    $mysql_host = "localhost";
    $mysql_database = "member";
    $mysql_user = "username";
    $mysql_password = "password";
    
    $cxn=mysql_connect($mysql_host,$mysql_user,$mysql_password);
    mysql_select_db($mysql_database);
    $rec=mysql_fetch_array(mysql_query("SELECT * FROM member WHERE loginName='$_GET[user_name]' AND pass = '$_GET[password]'"));
    if(($rec['loginName']==$_GET["user_name"])&&($rec['pass']==$_GET["password"]))
    {
    //do what you want to do if the user login is correct.
    $_SESSION['auth'] = "yes";
    $_SESSION['loginName'] = $_GET['user_name'];
    header("Location:member page url");
    ?>
    ________________________________________
    on top of every page that you want to protect, insert this:
    Code:
    <?php session_start();
    if(@$_SESSION["auth"]!= "yes"){header("Location:login.php");}?>
    ________________________________________
    thats it for the login tutorial, read my next post for a registration tutorial.

    See if it helps!

  9. #16
    Join Date
    Jul 2009
    Location
    Coquitlam BC Canada
    Posts
    46
    Thanks
    0
    Thanked 4 Times in 4 Posts

    Default

    oh well, i guess you dont need a registration tutorial.
    i type all that for nothing...

  10. #17
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    Eureka!!! guestbook WORKS!!! thanks.. again also i want to ask about this session system... it looks good and i understand it, but i created database with table: members. in it are password and username (exactly as i wrote). What i have to change then? (p.s. you starting new session in every page or just in login)
    Last edited by auriaks; 08-10-2009 at 01:20 AM.

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

    Default

    Chg table members to member, and then within it chg username to loginName and password to pass as per how fg123 wrote the code.
    - Josh

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

    Default

    Actually, it's easier just to DROP table members, and then copy+paste this SQL query into your mysql admin area:

    Code:
    CREATE TABLE `test`.`members` (
    `id` INT NOT NULL ,
    `loginName` VARCHAR( 50 ) NOT NULL ,
    `pass` VARCHAR( 255 ) NOT NULL ,
    `createDate` DATETIME NOT NULL ,
    PRIMARY KEY ( `id` ) 
    ) ENGINE = InnoDB
    - Josh

  13. #20
    Join Date
    Aug 2009
    Posts
    399
    Thanks
    42
    Thanked 4 Times in 4 Posts

    Default

    about sessions... you wrote
    PHP Code:
    <?php session_start();
    if(@
    $_SESSION["auth"]!= "yes"){header("Location:login.php");}?>
    that means i have to start session in every page i want to make safe?
    then, is it nessesary symbol @???
    And finally, process.php ypu wrote me... i using that, but didnt see where is action which directs to process.php... THANKS

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
  •