SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Passing parameters in php

    I've used a field username on one page, $username, the user enters his username into textbox. After that script checks if the password is correct. After that i want to redirect user to another page. And the parameter is gone. PHP does no longer remember what $username was from previous page. How do i make it remember $username provided in textbox?

    Should i use cookies, or is there something more basic to pass the value to another page? Value from textbox...

    Thank you.

  2. #2
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can pass variable over $_GET.
    -- Jelena --

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can u give me an example of code man plz?

  4. #4
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Provide your existing code, so we could help you man.


    Woman
    -- Jelena --

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jelena
    Provide your existing code, so we could help you man.


    Woman
    jelena... curo.....
    pozdrav iz pule

  6. #6
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by reminder
    jelena... curo.....
    pozdrav iz pule
    Hello 2 u 2 from Novi Sad
    -- Jelena --

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cd.bromley.ac.uk/olegs

    username: Grymauch
    password: oleg

    Script checks if password is correct then it redirects to another page.


    The page i'm working on now:

    http://cd.bromley.ac.uk/olegs/disp_info.php

    Code i'm trying to do: echo "you are logged in as $username";


    You guys speak russian or what?

  8. #8
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    How your script knows that user is logged in?

    I understand that you check if password is correct but that is not enough.

    You must somehow remember if he is logged in or not, using cookies or session.

    With cookies, after you check if password is ok, set cookie with user ID or even with username then you can retrieve it wherever you want it..

    I prefer using session but if you are new at it, use cookie.


    PS. It's not russian, it serbian and croatian
    -- Jelena --

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    if(isset($_POST["enterL"])) {
    clear_login(); 
    $username $_POST["username"];
    $password md5($_POST["password"]);
    $ret mysql_query("SELECT * FROM user WHERE username = '$username' AND password = '$password'");
    if(!
    $ret) die(mysql_error());
    $buffer mysql_num_rows($ret);

    if(
    $buffer 0) {
    $row mysql_fetch_row($ret);
    $_SESSION["logged"] = 1;
    $_SESSION["id"] = $row[0];
    $_SESSION["username"] = $row[1];
    $_SESSION["name"] = $row[2];
    $_SESSION["surname"] = $row[3];
    } else {
    echo 
    "Invalid username or password!";
    }
    }

    function 
    clear_login() {
    $_SESSION["logged"] = 0;

    cheers

  10. #10
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    London
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm russian,

    At the moment if the password is correct i set variable $log to true. so i have another if statement if $log==true, then you can continue.. etc

    I've never done cookies though. Can you explain how to do it plz?

    that script is checking whether the password is correct.

    <?php


    // connect to the mysql server
    $conn = @mysql_connect("localhost","user","pass")
    or die ("Could not connect to mysql table");

    // select the database
    $rs=@mysql_select_db("db",$conn)
    or die ("Could not select database");

    //create query
    $sql="select * from tblUsers where username='$username' and
    password='$password'";

    $rs=mysql_query($sql,$conn) or die(mysql_error());

    if($rs && mysql_num_rows($rs))
    {
    header('Refresh: 1; URL="disp_info.php"');
    $log=true;
    $username='username';
    }
    else
    {
    echo "Combination Username/Password is incorrect. Re-enter!";
    $log=false;
    }

    ?>

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2004
    Posts
    1,647
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    // connect to the mysql server
    $conn = @mysql_connect("localhost","user","pass")
    or die (
    "Could not connect to mysql table");

    // select the database
    $rs=@mysql_select_db("db",$conn)
    or die (
    "Could not select database");

    //create query
    $sql="select * from tblUsers where username='$username' and
    password='
    $password'";

    $rs=mysql_query($sql,$conn) or die(mysql_error());

    if(
    $rs && mysql_num_rows($rs))
    {
    header('Refresh: 1; URL="disp_info.php"');
    $_SESSION["log"] = 1;
    $_SESSION["username"] = $username# couse u have to pass sessions to another page...
    }
    else
    {
    echo 
    "Combination Username/Password is incorrect. Re-enter!";


  12. #12
    SitePoint Wizard silver trophy Jelena's Avatar
    Join Date
    Feb 2005
    Location
    Universum, 3rd Corner
    Posts
    3,000
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Here you set username in session
    PHP Code:
    <?php
    //start session
    session_start();
    // connect to the mysql server
    $conn = @mysql_connect("localhost","user","pass")
    or die (
    "Could not connect to mysql table");

    // select the database
    $rs=@mysql_select_db("db",$conn)
    or die (
    "Could not select database");

    //create query
    $sql="select * from tblUsers where username='$username' and
    password='
    $password'";

    $rs=mysql_query($sql,$conn) or die(mysql_error());

    if(
    $rs && mysql_num_rows($rs))
    {
    $_SESSION['username'] = $username;
    header('Refresh: 1; URL="disp_info.php"');
    }
    else
    {
    echo 
    "Combination Username/Password is incorrect. Re-enter!";
    $log=false;
    }

    ?>
    On this page you retrieve username
    PHP Code:

    session_start
    ();

    if(isset(
    $_SESSION['username']))
    {
       
    $username $_SESSION['username'];
       echo 
    $username;
    }
    else
    {
      echo 
    "You are not logged in.";

    BUT, this is not a very good solution. If you are creating user system, I would highly recommend to you to find tutorial on that subject and read them. If you google for php user authentication tutorial, I'm sure you'll find something usefull.
    ALSO, you should use Header('Location:') instead of Header('Refresh:...').
    -- Jelena --


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
  •