Passig variables to function ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • .: DeVa :.

    #1

    Passig variables to function ?

    I made a script for loging into members area and have one problem im using
    functions() to declare procedures to do...
    And when i want to use some variable that is out of the function it doesent
    work.. WHY? and how could i make it work..

    Example is bellow... i wont to use $username in functio logedin() but that
    doesent seems to work..

    CODE: (problem is marked with comment)

    <?php
    if(!isset($_SES SION['login'])){
    if(isset($_POST['username']) && (isset($_POST['password'])))
    {
    if (empty($usernam e) || empty($password )){
    echo "<p><p>";
    echo "<script>alert( 'Niste unjeli korisnièko ime ili zaporku')</script>";
    echo "<div align='center'> <a href='login_lin k.php'>Poku¹ajt e
    ponovo</a></div>";
    echo "<p>";
    echo "<div align='center'> Ako ste zaboravili zaporku mo¾ete je saznati <a
    href='zaboravlj ena_zaporka_lin k.php'>ovdje</a>";
    exit;
    }
    user_login();
    }elseif(isset($ _REQUEST['logout']))
    {
    unset($_SESSION['login']);
    session_destroy ();
    echo "logged Out <a href='$PHP_SELF '>login</a>";
    }else{
    display_login_f orm();
    }
    }else{
    logedin();
    }

    function user_login(){

    $database="pods vij_ribepret";
    mysql_connect(' localhost','pod svij','hrb3hv6g vr');
    @mysql_select_d b($database) or die( "Trenutno se nije moguæe konektirati na
    bazu!!");

    $query = "SELECT korisnik,sifra FROM users WHERE korisnik='" .
    $_POST['username'] . "'";
    $result = mysql_query($qu ery);
    if(!$result) die('Trenutno se nije moguce konektirati na bazu!!');
    $this_user = mysql_fetch_row ($result);

    if(($_POST['username'] == $this_user[0]) && ($_POST['password'] ==
    $this_user[1])){

    session_registe r('login');
    $_SESSION['login'] = 1;

    logedin();

    }else{

    display_login_f orm();
    }
    }

    function display_login_f orm()
    {
    echo "<div align='center' class='velika_s lova'>Ako niste registrirani
    korisnik, mo¾ete se registrirati <a
    href='registrac ija_link.php'>o vdje</a>.</div>";

    echo "<div align='center'> <form method='post' action='$PHP_SE LF'>";
    echo "Korisnièko ime";
    echo "<br><input type='text' name='username' >";
    echo "<br>Zapork a";
    echo "<br><input type='password' name='password' >";
    echo "<br><input type='submit' value=' Uðite '>";
    echo "</form></div>";

    echo "<div align='center'> Ako ste zaboravili zaporku mo¾ete je saznati <a
    href='zaboravlj ena_zaporka_lin k.php'>ovdje</a>";


    }


    //HERE IS THE PROBBLEM !!!
    function logedin()
    {
    echo "$username" ; // DOES NOT WORK!!
    echo "<div class='velika_s lova'>Dobrodo¹l i na korisnièke stranice portala
    www.podvodni-svijet.com..</div>";
    echo "<br><img src='admin/ikone/prica.gif' width='32' height='32'
    align='middle'> <a href='unos_pric a_korisnici_lin k.php'>&nbsp;Do daj ribolovnu
    prièu</a>";
    }


  • Jan Pieter Kunst

    #2
    Re: Passig variables to function ?

    In article <c5c0gj$6uk$1@b agan.srce.hr>,
    ".: DeVa :." <FAKEvedran@FAK Epodvodni-svijet.com> wrote:
    [color=blue]
    > Example is bellow... i wont to use $username in functio logedin() but that
    > doesent seems to work..[/color]

    Where is $username supposed to get its value from?

    If $username is supposed to be the same as $_POST['username'], this will
    work:

    // function call:
    logedin($_POST['username']);

    // function definition:
    function logedin($userna me) {

    }

    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    • .: DeVa :.

      #3
      Re: Passig variables to function ?

      Nop. i found what seems to be the problem, i had to use global $username...

      ------


      "Half-brother in blood,
      full brother in heart will I be.
      Thou shalt lead and I will follow.
      May no new grief divide us."

      Valar morghulis!
      --
      Remove all FAKEs when replying to mail !



      Jan Pieter Kunst natipka¹e:[color=blue]
      > In article <c5c0gj$6uk$1@b agan.srce.hr>,
      > ".: DeVa :." <FAKEvedran@FAK Epodvodni-svijet.com> wrote:
      >[color=green]
      >> Example is bellow... i wont to use $username in functio logedin()
      >> but that doesent seems to work..[/color]
      >
      > Where is $username supposed to get its value from?
      >
      > If $username is supposed to be the same as $_POST['username'], this
      > will work:
      >
      > // function call:
      > logedin($_POST['username']);
      >
      > // function definition:
      > function logedin($userna me) {
      >
      > }
      >
      > JP[/color]


      Comment

      • Jan Pieter Kunst

        #4
        Re: Passig variables to function ?

        In article <c5c9pb$d9s$1@b agan.srce.hr>,
        ".: DeVa :." <FAKEvedran@FAK Epodvodni-svijet.com> wrote:
        [color=blue]
        > Nop. i found what seems to be the problem, i had to use global $username...[/color]

        That's a possible way to do it. But I find that using function
        parameters instead of globals is more readable and leads to code that is
        easier to maintain.

        JP

        --
        Sorry, <devnull@cauce. org> is een "spam trap".
        E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

        Comment

        Working...