?
Solved

php help

Posted on 2003-03-12
10
Medium Priority
?
241 Views
Last Modified: 2008-02-26
Hi,
   I am new to php and am having some problem with it. I am trying to make an address book sort of a thing. There is hyperlink in my page...the link is to the page itself and an id gets transferred through the link. I obtain the id by using
$id=$_GET['id'];

This works but not when the page is opened for the first time(i.e when the id has empty value). I get this on my page
Notice: Undefined index: id in C:\Webdev\AddressBook\firstpage.php on line 18

However,when the id is passed the notice doesnt come. Do you guys have any idea on this one. It would be of great help if so. So then till next time take care and happy coding.
Sid :)

   
               
0
Comment
Question by:Sid123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 1

Expert Comment

by:PCBackup
ID: 8125224
In PHP, any undefined variables cannot be referenced in any statement, unless you precede them with an "@" character, the "at" sign, as in

  print @$id;

This allows the script to gracefully attempt the reference without generating the error.  You showed that you get the error on line #18, which is probably later in the script than the line you provided...

  $id=$_GET('id');

In your script, this line probably returns a null result and does not set the $id variable.  Then when you try to reference it, it generates the error.

You can either test for this case and set it to some value that can be tested or use the @ prefix to prevent the error.  You can perform a test for a good value with something like this...

  if (@$id <> "") {
     ...a functional program block...
    }

That will prevent any further errors from not having a value for $id.

Gene
0
 
LVL 6

Accepted Solution

by:
carchitect earned 80 total points
ID: 8125809
hi use this code

if(isset($_GET['id']))
$id=$_GET['id'];
else
$id=""; //whatever you want
0
 
LVL 6

Expert Comment

by:carchitect
ID: 8125810
use this code on top of the page
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Expert Comment

by:jfisbein
ID: 8127317
You can change you php.ini settings to not show NOTICE errors.

Change your "error reporting" conf to:
error_reporting = E_ALL & ~E_NOTICE

Or you can put an "@" before call to an statement that can cause an error.
ex:

@$id=$_GET['id'];
0
 
LVL 15

Expert Comment

by:VGR
ID: 8131973
follow carchitect's suggestion
0
 
LVL 1

Expert Comment

by:PCBackup
ID: 8133780
We have not heard back from Sid123 yet, so let's just wait and see what he (?) ends up deciding.

I personally don't like setting variables that I don't need, so I prefer to use the @ method to just prevent errors during execution.  Forcing the variable to be set to something just uses up extra server memory, and in a large scale web server, that's going to be important when you have many visitors all accessing pages at the same time.

Ultimately, the PHP programmer must decide what's most important to them.

Gene
0
 

Author Comment

by:Sid123
ID: 8133994
Hi all,
   Thank you very much for the help. I have tried out isset and it's working all right.However, I have come up with a new problem using session variables. I have two pages firstpage.php and firstpage1.php.The codes are as following-

--firstpage1.php--
 <? session_start();
  session_register("strError");
  $strError="1";

   if($name=="")
   { $strError ="Please enter the name!";
     $redirect = "firstpage.php";
     return header("location:$redirect");
     }
      ?>
--firstpage.php--
 <? session_start();
  session_register("strError");?>

<font color=#FF0000 size="4">&nbsp;<? if ($strError != "1")
                    echo $strError;     ?></font>

however I get this
Notice: Undefined variable: strError in C:\Webdev\AddressBook\firstpage.php on line 41
Notice: Undefined variable: strError in C:\Webdev\AddressBook\firstpage.php on line 42

From what I came to understand this is some sort of a bug...something to deal with the versions but I am not very sure about it. What do you guys say...how should I go about getting my codes right? So till next time take care and happy coding.

Sid:)

0
 
LVL 6

Expert Comment

by:carchitect
ID: 8134071
file1.php

<?
session_start();
str="";
session_register('str');
$_SESSION['str']="anything";
header("Location: file2.php");
?>


file2.php
<?
session_start();
echo $_SESSION['str'];
?>

and whenever use session variable use $_SESSION['variablename'];
0
 

Author Comment

by:Sid123
ID: 8145819
Hi,
  I tried to use the mail function.The thing is I am pretty vague about this.Can I use it to send email using the local host or is it possible only when the file is uploaded. I read somewhere that you can send email using local host too...but you need an SMTP server...and I have downloaded it(freeware). I tried to mail to "someone@yahoo.com" but and getting this-

Warning: mail() [function.mail]: SMTP server response: 550 User not local. We don't relay in C:\Webdev\AddressBook\email1.php on line 10

Can anyone give me any idea on this one? So then till next time take care and happy coding.
Sid:)
0
 
LVL 15

Expert Comment

by:VGR
ID: 8145896
yes, that's 100% normal. The "From:" address you specified has to be verified and validated by your SMTP server (the one you talk to with mail(), specified in the php.ini [mail] section, at least on Windows, and in server's configuration on *nix)

This address HAS TO BE VALID, ie to belong to your domain.

Otherwise it would be the wide-open door to would-be-spammers everywhere in the world, sending email as from "nobody-dont-even-search@nonexistentdomain.com"

understood ? :D
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question