I'm new to php and right now i'm doing a server management script for server admins. I would like to include some server information in this page and as part of the information I like to include the CPU Name and i did the script.

<?php

    $cpuinfo = file("/proc/cpuinfo");

    for ($i = 0; $i < count($cpuinfo); $i++)
            {
                    list($item, $data) = split(":", $cpuinfo[$i], 2);
                    $item = chop($item);
                    $data = chop($data);
                    if ($item == "processor") {$total_cpu++; }
                    if ($item == "vendor_id") { $vendor_id = $data; }
                    if ($item == "model name") { $cpu_name = $data; }
                    if ($item == "cpu MHz") {$cpu_speed = " " . floor($data);}
            }

    $cpu_info = $total_cpu.'x '.$vendor_id.' '.$cpu_name.' '.$cpu_speed." MHz\n";

    $cpu = "<p>CPU :".$cpu_info."</p>";

    echo $cpu;
?>

when running this script it displays the cpu version but it displays these errors below.

Notice: Undefined variable: total_cpu in /var/www/html/daniel/index.php on line 10 Notice: Undefined offset: 1 in /var/www/html/daniel/index.php on line 7 Notice: Undefined offset: 1 in /var/www/html/daniel/index.php on line 7 Notice: Undefined offset: 1 in /var/www/html/daniel/index.php on line 7 Notice: Undefined offset: 1 in /var/www/html/daniel/index.php on line 7

CPU :4x GenuineIntel Intel(R) Xeon(R) CPU X3360 @ 2.83GHz 2825 MHz

Anyone know how to solve the problem?

    For one, you never define and initialize $total_cpu before you attempt to increment it.

    For another, you never verify inside the loop that $cpuinfo[$i] actually contains a ':' before attempting to split() on that value.

    Finally, note that [man]split/man is a regular-expression function and has been deprecated; you should instead be using [man]explode/man.

      Notice: Undefined variable: total_cpu in /var/www/html/daniel/index.php on line 10

      problem 1 solved thanks.

      for the second problem i still did not understood what you

        Here's what I want you to do: Copy and paste all of the text to the right of the ":" character (a colon) in the string...

        Hello world!

        ... and post it as a reply.

          thanks for you help. I did a new code that worked perfectly now 🙂

          <?php
          
          $cpuinfo = file("/proc/cpuinfo");
          
          $total_cpus = count($cpuinfo)/25;
          
          $cpuinfo = exec("cat /proc/cpuinfo", $data);
          
          list($item, $data) = split(":", $data[4], 2);
          $item = chop($item);
          $model = chop($data);
          
          $cpuinfo = exec("cat /proc/cpuinfo", $data);
          
          list($item, $data) = split(":", $data[6], 2);
          $item = chop($item);
          $speed = chop($data);
          
          $cpuinfo = exec("cat /proc/cpuinfo", $data);
          
          list($item, $data) = split(":", $data[7], 2);
          $item = chop($item);
          $cache = chop($data);
          
          $cpu_info = $total_cpus."x ".$model." ".$speed;
          
          echo $cpu_info;
          
          ?>

            Except now you're "new code" isn't flexible at all and still has the same problems I mentioned above. You might as well just do:

            <?php
            
            echo "CPU :4x GenuineIntel Intel(R) Xeon(R) CPU X3360 @ 2.83GHz 2825 MHz";
            
            ?>

            and be done with it.

            Or you could address the issues I noted above, such as by using [man]strpos/man to first check if a string actually contains the ":" character before you try to split the string.

              well it worked on another virtual server too. I installed ubuntu server on a virtual host on my laptop and after installing apache, php, ftp bla bla, I uploaded the script and got a good result.

              CPUs	2x Intel(R) Core(TM)2 Duo CPU T6500 @ 2.10GHz (2.100GHz)[/code[
                Write a Reply...