Pages: 12Next
Current Page: 1 of 2
Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: October 24, 2009 12:50AM

I'm a beginner that want to learn PHP.

I startet a guide at W3school and early I met some problems.

It works when I use this code:
<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html>



But this code doesn't work:
<?php
$txt="Hello World!";
$x=16;
?>

or this: (Swedish guide) (In thisone below I get error message on lines)
<?php

$heltal = 1;

$flyttal = 1.22;

$bokstav = a;

$strang = Fredrik Johansson;

$vektor = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

echo Då var tilldelningen klar, inga felmeddelanden genererade?;

?>



What is the problem here?

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: stevenmartin99 (---.b-ras2.blp.dublin.eircom.net)
Date: October 24, 2009 05:31AM

<?php
$txt="Hello World!";
$x=16;
?>


this doesnt do anything.... ul just get a blank page

you need to echo information to the screen like this


--------------------------------------------------------

<?php
$txt="Hello World!";
$x=16;
echo $x;
echo '<br>';
echo $txt;
?>
---------------------------------------------------------

<?php
$txt="Hello World!";
$x=16;
echo $x.'<br>'.$txt;
?>
---------------------------------------------------------







<?php

$heltal = 1;

$flyttal = 1.22;

$bokstav = a;

$strang = Fredrik Johansson;

$vektor = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

echo Då var tilldelningen klar, inga felmeddelanden genererade?;

?>


this one will cause an error as there is a mistake. you need ' ' around the echo

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: toumimi (---.72.113-78.rev.gaoland.net)
Date: October 24, 2009 10:53AM

Not only echo function...
You need to put quotes (single or not) to every string you use.

$bokstav = 'a';
$strang = 'Fredrik Johansson';
echo 'Då var tilldelningen klar, inga felmeddelanden genererade ?';

Florian

WampServer Patch (Screenshots)
Topic EN : www.wampserver.com
Topic FR : www.wampserver.com

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: October 30, 2009 02:27PM

Thanks, but how does it work when I want to use:

<?php

define(BGCOLOR, #FFFFFF');

echo BGCOLOR;

?>

Why wont that code work for me?

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: stevenmartin99 (86.40.55.---)
Date: October 30, 2009 03:51PM

what are you expecting to happen?

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: c2dan (---.15-1.cable.virginmedia.com)
Date: October 30, 2009 06:36PM

Your code wont work as you have left of the ' before the # on this line
define(BGCOLOR, #FFFFFF');

Remember all characters, except numbers (eg 1-9 etc), floats (1.234 etc) and booleans (true or false etc) need to be wrapped within quotes.

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: October 30, 2009 10:52PM

Thanks!

Before I new anything about PHP I made a page with HTML and CSS.

I have a "become a member" and a "Logg in".

Now I have started to learn about PHP. How do I use it to my site?
I need a place to store the users that want to be members. I need to make the logg-in to work.

What should I build my site with? Should I use .PHP or .HTM and how will it work?

Can you use .PHP and use normal HTML-codes in the .PHP-doc? Should I change the .HTM I got to .PHP?

How do I store the members and make the "become a member" and "logg in" to work?

I'm about to make a text-based online game (belive me or not!) and later I will need to make a slump-generator to work when games against members will be played. What codes should I need for this?

Thanks a lot,

SecTunez

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: stevenmartin99 (86.40.55.---)
Date: October 30, 2009 10:55PM

you wont be doing any of that for about 4 months smiling smiley youve a lot to learn about php, no one can show you , you really just need to try and learn

Steven Martin
stevenmartin99@gmail.com
stevenmartin99@hotmail.com
PampServer.com - [pampserver.com]

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: October 31, 2009 12:02AM

Yeah that's what I thought. But I didn't thought it was to hard to make a Become a member and a logg-in to work. I know I have a lot to learn, offcourse, but where should I start and in what steps do I need to learn PHP?

I've read some beginners guides for the last couple of weeks, but still don't know almost anything. I've just tried some codes during the guides but I still haven't learned how PHP and HTML works together and what I should build with what...

I've read some on W3schools.com, PHPportalen.net (swedish guides), hotscripts.com etc.

Would I be able to make the whole site without having a domain?
Because I think it would be good if I could wait to buy a domain so I don't pay for a domain when my site wouldn't be done in a couple of years...

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: November 04, 2009 02:17PM

Is there any simple guides (that works) for creating a registration/login in that is stored in my DB?

I've created the table in MySQL:

CREATE TABLE users (
user_id INT auto_increment,
user_email VARCHAR(250),
user_password VARCHAR(200),
user_hash VARCHAR(200),
user_active INT,
user_full_name VARCHAR(250),
PRIMARY KEY (user_id)
) TYPE=MyISAM ;


I have a registration and log-in form in .htm where action-button proceeds to a reg.php or login.php.

I don't know whats wrong in my diffrent .php-docs. I've tried diffrent guides but can't make it work.

When I connect to database using login name and password, wich should I use? First of all, I can logg on to phpMyAdmin with usename: root, password: password.
Then I have created a database:
dbname: test
dbusername: test
dbpassword: test

What is the right thing to write in this code then:
$db=mysql_connect('localhost','loginuser','loginpass') or die(mysql_error());
mysql_select_db('logindb');

should "'loginuser','loginpass'" be "'root','password'" or "'test','test'" ?


All this with registration and log-in should work with only local connection or do I need a real site?

When I try to register some accounts they don't pop up in the SQL-table...


Thanks,
SecTunez

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: yfastud (Moderator)
Date: November 05, 2009 01:28AM

it should be 'test','test' and to be exact, your sample script above should be as follows:

$db=mysql_connect('localhost','test','test') or die(mysql_error());
mysql_select_db('test');

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: November 05, 2009 11:04PM

Thanks, thats what I've been used but it still doesn't work.

Now I've tried an even simplier code but it still dosen't work.

I have this table:
CREATE TABLE users (
username varchar(30),
password varchar(32));


I have a database.php with the code:
<?

/**
* Connect to the mysql database.
*/
$conn = mysql_connect("localhost", "test", "test"winking smiley or die(mysql_error());
mysql_select_db('test', $conn) or die(mysql_error());

?>


Then I have the registration:

<?
session_start();
include("database.php"winking smiley;

/**
* Returns true if the username has been taken
* by another user, false otherwise.
*/
function usernameTaken($username){
global $conn;
if(!get_magic_quotes_gpc()){
$username = addslashes($username);
}
$q = "select username from users where username = '$username'";
$result = mysql_query($q,$conn);
return (mysql_numrows($result) > 0);
}

/**
* Inserts the given (username, password) pair
* into the database. Returns true on success,
* false otherwise.
*/
function addNewUser($username, $password){
global $conn;
$q = "INSERT INTO users VALUES ('$username', '$password')";
return mysql_query($q,$conn);
}

/**
* Displays the appropriate message to the user
* after the registration attempt. It displays a
* success or failure status depending on a
* session variable set during registration.
*/
function displayStatus(){
$uname = $_SESSION['reguname'];
if($_SESSION['regresult']){
?>

<h1>Registered!</h1>
<p>Thank you <b><? echo $uname; ?></b>, your information has been added to the database, you may now <a href="main.php" title="Login">log in</a>.</p>

<?
}
else{
?>

<h1>Registration Failed</h1>
<p>We're sorry, but an error has occurred and your registration for the username <b><? echo $uname; ?></b>, could not be completed.<br>
Please try again at a later time.</p>

<?
}
unset($_SESSION['reguname']);
unset($_SESSION['registered']);
unset($_SESSION['regresult']);
}

if(isset($_SESSION['registered'])){
/**
* This is the page that will be displayed after the
* registration has been attempted.
*/
?>

<html>
<title>Registration Page</title>
<body>

<? displayStatus(); ?>

</body>
</html>

<?
return;
}

/**
* Determines whether or not to show to sign-up form
* based on whether the form has been submitted, if it
* has, check the database for consistency and create
* the new account.
*/
if(isset($_POST['subjoin'])){
/* Make sure all fields were entered */
if(!$_POST['user'] || !$_POST['pass']){
die('You didn\'t fill in a required field.');
}

/* Spruce up username, check length */
$_POST['user'] = trim($_POST['user']);
if(strlen($_POST['user']) > 30){
die("Sorry, the username is longer than 30 characters, please shorten it."winking smiley;
}

/* Check if username is already in use */
if(usernameTaken($_POST['user'])){
$use = $_POST['user'];
die("Sorry, the username: <strong>$use</strong> is already taken, please pick another one."winking smiley;
}

/* Add the new account to the database */
$md5pass = md5($_POST['pass']);
$_SESSION['reguname'] = $_POST['user'];
$_SESSION['regresult'] = addNewUser($_POST['user'], $md5pass);
$_SESSION['registered'] = true;
echo "<meta http-equiv=\"Refresh\" content=\"0;url=$HTTP_SERVER_VARS[PHP_SELF]\">";
return;
}
else{
/**
* This is the page with the sign-up form, the names
* of the input fields are important and should not
* be changed.
*/
?>

<html>
<title>Registration Page</title>
<body>
<h1>Register</h1>
<form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="subjoin" value="Join!"></td></tr>
</table>
</form>
</body>
</html>


<?
}
?>


When I then tries to registrate a new account through: [localhost]
it leads me to:
[localhost][%27PHP_SELF%27];%20?%3E

And it says:
Forbidden

You don't have permission to access /< on this server.


Everything in the guide is from [www.evolt.org]
The is all codings I've used and all the 7 pages...

What is the problem for me that NO guide have worked for me? What is wrong? I've fallowed the guides and have everything in
wamp\www\
and I have a table in MySQL through phpMyAdmin in my database called test...


Thanks,

SecTunez

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: November 05, 2009 11:09PM

Corrections for the links in my previous post:

"When I then tries to registrate a new account through: [localhost]
it leads me to:
[localhost][%27PHP_SELF%27];%20?%3E "

[localhost] = h t t p : / / l o c a l h o s t / r e g i s t e r . p h p

[localhost][%27PHP_SELF%27];%20?%3E = h t t p : / / l o c a l h o s t / % 3 C ? % 2 0 e c h o % 2 0 $ H T T P _ S E R V E R _ V A R S


Why am I comming to "h t t p : / / l o c a l h o s t / % 3 C ? % 2 0 e c h o % 2 0 $ H T T P _ S E R V E R _ V A R S" after I've tried to registrate a user?


Thanks,

SecTunez

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: yfastud (Moderator)
Date: November 06, 2009 01:27AM

to setup db, follow this
[blog.jlbn.net]

then use this
$db=mysql_connect('localhost','test','test') or die(mysql_error());
mysql_select_db('test');

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: November 08, 2009 05:16PM

I have set up a database and I fallow now this guide to create a log-in/reg:
[www.evolt.org]
h t t p : / / w w w . e v o l t . o r g / n o d e / 6 0 3 8 4

I've changed in 'constants.php'

/**
* Database Constants - these constants are required
* in order for there to be a successful connection
* to the MySQL database. Make sure the information is
* correct.
*/
define("DB_SERVER", "localhost"winking smiley;
define("DB_USER", "your_name"winking smiley;
define("DB_PASS", "your_pass"winking smiley;
define("DB_NAME", "your_dbname"winking smiley;

TO:

/**
* Database Constants - these constants are required
* in order for there to be a successful connection
* to the MySQL database. Make sure the information is
* correct.
*/
define("DB_SERVER", "localhost"winking smiley;
define("DB_USER", "test"winking smiley;
define("DB_PASS", "test"winking smiley;
define("DB_NAME", "test"winking smiley;


But still it dosen't work.

Theres a lot of text and codes in "localhost / registration.php" and when I registrate a member i comes to
"localhost / process.php"
and there it comes all text:

"
procLogin(); } /* User submitted registration form */ else if(isset($_POST['subjoin'])){ $this->procRegister(); } /* User submitted forgot password form */ else if(isset($_POST['subforgot'])){ $this->procForgotPass(); } /* User submitted edit account form */ else if(isset($_POST['subedit'])){ $this->procEditAccount(); } /** * The only other reason user should be directed here * is if he wants to logout, which means user is * logged in currently. */ else if($session->logged_in){ $this->procLogout(); } /** * Should not get here, which means user is viewing this page * by mistake and therefore is redirected. */ else{ header("Location: main.php"winking smiley; } } /** * procLogin - Processes the user submitted login form, if errors * are found, the user is redirected to correct the information, * if not, the user is effectively logged in to the system. */ function procLogin(){ global $session, $form; /* Login attempt */ $retval = $session->login($_POST['user'], $_POST['pass'], isset($_POST['remember'])); /* Login successful */ if($retval){ header("Location: ".$session->referrer); } /* Login failed */ else{ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: ".$session->referrer); } } /** * procLogout - Simply attempts to log the user out of the system * given that there is no logout form to process. */ function procLogout(){ global $session; $retval = $session->logout(); header("Location: main.php"winking smiley; } /** * procRegister - Processes the user submitted registration form, * if errors are found, the user is redirected to correct the * information, if not, the user is effectively registered with * the system and an email is (optionally) sent to the newly * created user. */ function procRegister(){ global $session, $form; /* Convert username to all lowercase (by option) */ if(ALL_LOWERCASE){ $_POST['user'] = strtolower($_POST['user']); } /* Registration attempt */ $retval = $session->register($_POST['user'], $_POST['pass'], $_POST['email']); /* Registration Successful */ if($retval == 0){ $_SESSION['reguname'] = $_POST['user']; $_SESSION['regsuccess'] = true; header("Location: ".$session->referrer); } /* Error found with form */ else if($retval == 1){ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: ".$session->referrer); } /* Registration attempt failed */ else if($retval == 2){ $_SESSION['reguname'] = $_POST['user']; $_SESSION['regsuccess'] = false; header("Location: ".$session->referrer); } } /** * procForgotPass - Validates the given username then if * everything is fine, a new password is generated and * emailed to the address the user gave on sign up. */ function procForgotPass(){ global $database, $session, $mailer, $form; /* Username error checking */ $subuser = $_POST['user']; $field = "user"; //Use field name for username if(!$subuser || strlen($subuser = trim($subuser)) == 0){ $form->setError($field, "* Username not entered
"winking smiley; } else{ /* Make sure username is in database */ $subuser = stripslashes($subuser); if(strlen($subuser) < 5 || strlen($subuser) > 30 || !eregi("^([0-9a-z])+$", $subuser) || (!$database->usernameTaken($subuser))){ $form->setError($field, "* Username does not exist
"winking smiley; } } /* Errors exist, have user correct them */ if($form->num_errors > 0){ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); } /* Generate new password and email it to user */ else{ /* Generate new password */ $newpass = $session->generateRandStr(8); /* Get email of user */ $usrinf = $database->getUserInfo($subuser); $email = $usrinf['email']; /* Attempt to send the email with new password */ if($mailer->sendNewPass($subuser,$email,$newpass)){ /* Email sent, update database */ $database->updateUserField($subuser, "password", md5($newpass)); $_SESSION['forgotpass'] = true; } /* Email failure, do not change password */ else{ $_SESSION['forgotpass'] = false; } } header("Location: ".$session->referrer); } /** * procEditAccount - Attempts to edit the user's account * information, including the password, which must be verified * before a change is made. */ function procEditAccount(){ global $session, $form; /* Account edit attempt */ $retval = $session->editAccount($_POST['curpass'], $_POST['newpass'], $_POST['email']); /* Account edit successful */ if($retval){ $_SESSION['useredit'] = true; header("Location: ".$session->referrer); } /* Error found with form */ else{ $_SESSION['value_array'] = $_POST; $_SESSION['error_array'] = $form->getErrorArray(); header("Location: ".$session->referrer); } } }; /* Initialize process */ $process = new Process; ?>
"

Should it be like that? It doesn't saves any user in phpMyAdmin under my table in the database test...

I'm getting so frustrated of this, I haven't acomplished anything in the latest couple of weeks now!

Please give me som guidness.

Thx,
SecTunez

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: yfastud (Moderator)
Date: November 08, 2009 08:31PM

it's coding problem so you should check w/ author or coding forums

anyway, first follow guide to setup db, then open that db to import file dbtables.sql, then edit file contansts.php w/ info from db, then you should have no problem as seen here; try it w/ username tester and passwd test

[jlbn.com]

you can also setup vh to assigned a particular dir to this login script

to setup vh, follow this
[blog.jlbn.net]

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides



Edited 1 time(s). Last edit at 11/08/2009 09:13PM by yfastud.

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: November 10, 2009 10:23PM

Thanks.

Yeah, I've already fallowed the guide to setup vh. But I don't understand the use of it?

Should I instead put my .php in the "domain"-folder? Is that the problem why my login- and registration-scripts haven't worked?

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: yfastud (Moderator)
Date: November 11, 2009 01:47AM

make it simple for you to understand, you use vh to assigned a specific dir to a specific domain, user and/or script, and in your case you want to setup for a script; for example, if you setup vh to have dir login, then extract your download script and put all files in this dir login and access as 'http://login'

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: SecTunez (---.79-161-75.customer.lyse.net)
Date: November 14, 2009 12:11AM

I've fallowed the guides again. Created dir login with the VH-guide and a db with db_name: tester and user test.
Downloaded the loginscript to the folder login. Created the table and then tried to add a member through 'http://login/register.php' but still no member shows up in the table in the db... FRUSTRATED.

But thanks for all help I've got from you yfastud.
I'm gonna keep tryin and update if something change smiling smiley


Thanks,
SecTunez

Options: ReplyQuote
Re: Beginner: I can't get some simple codes
Posted by: yfastud (Moderator)
Date: November 14, 2009 06:39AM

you've got it wrong and that's why it's not working, so here are steps

1. follow this guide to setup db, for example, db "login", username "login" and password "loggedin"
[blog.jlbn.net]
2. click to open that db "login"
3. click the button "Import" and browse to the file dbtables.sql in the extract folder downloaded from 'http://www.evolt.org/node/60384'
4. open to edit file contansts.php w/ info from db, ie, db "login", username "login" and password "loggedin"
5. if also followed vh guide, then you should have no problem to access 'http://login'

Have fun,

FREE One A Day
FREE Photo
FREE Games
FREE Websites
FREE Portable GPS
FREE WAMP Guides

Options: ReplyQuote
Pages: 12Next
Current Page: 1 of 2


Sorry, only registered users may post in this forum.