Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
hey all i am such newbie for creating web pages... on my pc i run apache and i wanted to create php file as it web page....i read some book ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34

    PHP problem


    hey all
    i am such newbie for creating web pages... on my pc i run apache and i wanted to create php file as it web page....i read some book about php and i tried to do it

    This is my code:
    <html>
    <head><title><?php echo "welcome to my site";?><title></head>
    <body>
    <form method="post">
    <p>SecretName:
    <input type="text" name="name" size="40" value=""/>
    </p>
    <input type="submit" name="submit" value="Go"/>
    </form>
    <?php

    $secretname="nik";
    $_POST['name']='name';
    if ($_POST['name']==$secretname)
    {
    echo "matched";
    }
    else{

    echo "not matched";}
    ?>
    </form>
    </body>
    </html>

    It's working well on the web site but it give error in apache's error log.
    This is what it shown in error.log:
    [Tue Oct 18 09:18:55 2011] [notice] Apache/2.2.14 (Ubuntu) PHP/5.3.2-1ubuntu4.10 with Suhosin-Patch configured -- resuming normal operations
    [Tue Oct 18 09:19:06 2011] [error] [client 192.168.101.86] PHP Notice: Undefined index: name in /var/www/test.php on line 12
    [Tue Oct 18 09:19:06 2011] [error] [client 192.168.101.86] PHP Stack trace:
    [Tue Oct 18 09:19:06 2011] [error] [client 192.168.101.86] PHP 1. {main}() /var/www/test.php:0
    [Tue Oct 18 09:19:06 2011] [error] [client 192.168.101.86] PHP Notice: Undefined index: name in /var/www/test.php on line 14
    [Tue Oct 18 09:19:06 2011] [error] [client 192.168.101.86] PHP Stack trace:
    [Tue Oct 18 09:19:06 2011] [error] [client 192.168.101.86] PHP 1. {main}() /var/www/test.php:0

    any ideas?i hope some php experts over here can help me..thanks in advanced

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    You didn't close the <title> tag properly - change the second title tag to </title>.

    Also, you are assigning the CGI variable to 'name', so you're not really testing it - remove that $_POST['name']='name'; line.

  3. #3
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    thanks for answering my question...i have tried what u told me to do such as remove $_POST['name']='name' and the <title> to </title> but the same error still appear on the error.log...any more ideas?thanks in advanced

  4. #4
    Linux Enthusiast scathefire's Avatar
    Join Date
    Jan 2010
    Location
    Western Kentucky
    Posts
    626
    Are you posting to the same page? I'm not an expert by any stretch, but what I usually do when I'm posting my results to the same page I have the code something like this:

    Code:
    <html>
    <title>blah</blah>
    <head></head>
    <body>
    <?php
    if (!$_POST['submit']) {
            ?>
    <form action="<?=$_SERVER['PHP_SELF']?>" method="POST">
    <input blah>
    <input blah>
    <input type="submit" name="submit" value="Blah">
    <?php
    }
    else {
    the rest of your code here
    }
    ?>
    </body>
    </html>
    linux user # 503963

  5. #5
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Don't know why you're getting that error, I'm not seeing it in my logs on my setup (Apache 2.2.17/PHP 5.3.6). Does the page otherwise work?

    Maybe try enabling the viewing of errors temporarily in your php.ini file.

  6. #6
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    It's not an major error but just a notice.
    It tells that in the page may be a bug since a value that doesn't exist is used.

    Your code:
    PHP Code:
    if ($_POST['name']==$secretname){
      echo 
    "matched";
    }else{
      echo 
    "not matched";

    When php tries to compare $_POST['name'] with the value of $secretname it tries to access the array $_POST
    The notice in your log just tells that $_POST was not set to anything.

    To avoid that notice write it like that:
    PHP Code:
    if (isset($_POST['name'])) if ($_POST['name']==$secretname){
      echo 
    "matched";
    }else{
      echo 
    "not matched";

    Note:
    not matched will only be shown if the $_POST value is at least set.

    You can also use a pre-load function like:
    PHP Code:
    //List of values allowed from POST method:
    $load_vars[]="name";
    $load_vars[]="password";

    //---LIST END
    //Store all allowed values into $load_val
    foreach ($load_vars as $name)
      if (isset(
    $_POST[$name])) $load_val[$name]=$_POST[$name];
      else 
    $load_val[$name]=false;
    unset(
    $load_vars$name);
    //--- Program:
    if ($load_val['name']==$secretname){
      echo 
    "matched";
    }else{
      echo 
    "not matched";


  7. #7
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    yessss....everything is fine now...thanks a lot guys....be a newbie is such a pain haha.....anyways thanks for all information and help....now no more error..

  8. #8
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    Hey guys,
    i really want to try something which i do not sure whether it can be done or not using php...This is what i want to try:
    For example i have a C program called try.c like this:
    #include <stdio.h>
    int main()
    {
    int a,b,c;
    printf("a:");
    scanf("%d",&a);
    print("b:");
    scanf("%d",&b);
    c=a+b;
    printf(""%d + %d=%d",a,b,c);
    return 0;
    }
    i compile this using gcc -o try try.c and ./try and then it will ask for the value of a and b.let say i give the value of a=2 and b=4 so c would be c=6.
    i found out on Internet that the php also can run the external program
    using shell execution (shell_exec(string $cmd)).
    So i try my php file below to run my c program on the web page:
    Below is My php file:

    <?php
    $output=shell_exec(./try);
    print "<p>$output</p>"
    ?>

    but when i open this .php on the web browser it doesnt asked for the value of a and b and it also give me a very weird value of c...do u guys have any ideas on how to make php work like the normal compiler which ask the user to give the value of a and b before it give the value of c?Thanks in advanced

  9. #9
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Have your PHP form take the argument as an input field (or select dropdown box or whatever) and once the form is submitted, get the argument that was entered and pass it to your try command.

    e.g., here, the input element is named "arg":
    Code:
    <form method="post">
    <p>Test
    <input type="text" name="arg" size="40" value=""/>
    </p>
    <input type="submit" name="submit" value="Go"/>
    </form>
    <?php
    echo "(Enter \"arg\" to test)<p>\n";
    if(isset($_POST['arg'])){
      $myarg=$_POST['arg'];
      if($myarg){ 
        echo "you entered: $myarg<BR>\n";
        $output=shell_exec("/tmp/try $myarg");
        echo "output is: $output<BR>\n";
      }
    }
    ?>
    Last edited by atreyu; 10-20-2011 at 03:54 PM. Reason: example

  10. #10
    Just Joined!
    Join Date
    Oct 2011
    Posts
    34
    i have just tried your code but it is not working as i wish.....
    for example try.c is like this:
    #include <stdio.h>
    void main (void)
    {
    int a;
    scanf("%d,&a);
    printf("a=%d",a);
    }
    i tried with php for executing it and this is the result on the web page:

    (Enter "arg" to test)

    you entered: 10
    output is: Calculation a:10530804
    My php file is similar to yours.but somehow when i entered 10, the output of a is not equal to 10 but it is equal to 10530804...so any more ideas...
    Last edited by nikhidet; 10-24-2011 at 09:41 AM.

Page 1 of 2 1 2 LastLast

Posting Permissions

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