Jump to content

[Guide] Basics in PHP (Beginners)


WizZy™

Recommended Posts

Hey guys!I`m bored and have nothing to do,so many people have problems with they'r site coding (php) and i wanted to let u know the basics..

First of all , most of it wants a SQL connection.You can create it using this :

<?php
$user = "root"; //Your MySQL user
$pass = "pass"; //Your MySQL Password
$db = "hack"; //Your MySQL database
$host = "localhost"; // Your MySQL address (its as default)
$connection = mysql_connect($host, $user, $pass) or die("Error with MySQL connecting"); // AIO connect
$db = mysql_select_db($db,$connection)or die("Error with MySQL connecting2"); // database connect
$chars="set character set latin1"; //set chars
mysql_query($chars); // mysql query for chars
?>

Its a simple SQL connector.So we are proceeding.. How to read SQL file thru web? Its simple one ,just configurate it by your needs.

But you need a config file like conf.php and including it in each page ,so this file can connect to mysql.

just type

<?php include "FILE.php"; ?> 

So here just change FILE to conf (like i said) and it will include it in each page you write that.

<?php
include "conf.php";

$query="SELECT * FROM reports ORDER by id DESC LIMIT 1"; // Selection of SQL table and column
$result=mysql_query($query); // Getting query
while ( $r = mysql_fetch_array( $result )){ // The result
$some1=$r['some1']; // Description
$some2=$r['some2']; // Description
$some3=$r['some3']; // Description
$some4=$r['some4']; // Description
echo"<br>$some1";
echo "<br>$some2";
echo "<br>$some3";
echo "<br>$some4";
}
?>

Easy one ,Huh?

So enough mysql.. Lets do some other basics.

Like in other sites they have a config.php file.Why do they need it?

I use it to make a called VARS.These are like

 <?php $var1 = "Hello world!I`m php!"; 
echo "$var1"; ?>

$var1 is the displaying text and this after it is the real text.I am using ECHO to get the var into a text.You can do same for other functions ,but this is basic.

Okay a simple protection i`ll post below.Everyone knows what it means and its useful for some pages you don't want users to view.Here is it

<?php
include('conf.php');
session_start();
$name[] = "user1"; $pass[] = "pass1"; // Username for login 1
$name[] = "user2"; $pass[] = "pass2"; // Username for login 2
//You can add as much users you want.

function loginform(){ //The form that unlogged users will use
echo '<fieldset style="padding: 2">';
echo '<legend>Login</legend>';
echo '<form method=post action="?">';
echo 'Name: <input type=text name=username value="' . $_POST[username] . '">'; //The name
echo '<br>';
echo 'Pass: <input type=password name=pass>'; //The password
echo '<br>';
echo '<input type=submit name="s1" value="Login">'; // The submit button
echo '</form>';
echo '</fieldset>';
}


if($_GET['action'] == "logout"){ // Logging out
$_SESSION['loggedin'] = false;
$_SESSION['username'] = "";
}

if($_SESSION['loggedin'] == false){ // Protection from NULL logins
if(!$_POST['s1']){
loginform();
}else{

$tmpname = $_POST['username']; #the username the user has submitted
$tmppass = $_POST['pass']; #the password the user has submitted

$t = count($name); #count the total users
$i = 0;

while($i <= $t){
if($tmpname == $name[$i] && $tmppass == $pass[$i]){ //Checking

$_SESSION['loggedin'] = true;
$_SESSION['username'] = $name[$i];
header('Location: ?'); #reload the page
}
$i++;
}
echo "Your attempt is invalid."; //If something is wrong
loginform(); #show the form
}
die; // if something is wrong , destroy it
}
?>

<center><br>Welcome <?php echo $_SESSION['username']; ?>
<br><a href="?action=logout">Logout</a> . </center>
<br>

I included some other things like Logout and stuff.. So anyway a simple code ,who records all IP's viewed the page you included it.

<?php
$file = fopen('ipadmin.txt', 'a'); // opens ,create a new file with storage
fwrite($file,$_SERVER['REMOTE_ADDR']."\n"); // Ip storing
fclose($file); // Close the file
echo "<br><b>Your ip was stored successfuly.</b>"; // if successful
?>

This will record every IP viewed your page in a file called ipadmin.txt . You can change it ofcourse.

Okay one more script.I called it Redirecting script ,because its redirecting u :D Here it is :

<?php
$url = ''; // ULR GOES HERE EDIT IT!
while (list($var,$val) = each($HTTP_GET_VARS))
$url .= "&$var=$val";
$i = strpos($url, "&url=");
if ($i !== false)
$url = substr($url, $i + 5);
if (substr($url, 0, 4) == "www.") 
$url = "http://" . $url;
print("<html><head><meta http-equiv=refresh content='0;url=$url'></head><body>\n");
print("<table border=0 width=100% height=100%><tr><td><h2 align=center>Currently moving to:<br>\n");
print("$url</h2></td></tr></table></body></html>\n");
?> 

It can be done alot easyer ,but this is with PHP.Html is ALOT easyer.

How to get some info's from another site? Another simple code.

<?php
$url="http://BLABLA.com/INFO.php"; // Edit this link to get the info from.
$string = file_get_contents($url);
$edno=explode('<div align="left">',$string);
$dve=explode('</div></td>',$edno[1]);
echo $dve[0];
?> 

 

Okay i think you learned the basics.. If you wanna just write them to remember them faster lol :D

Lol i`m making this topic about 16 mins :D Anyway thanks for viewing it.

I`ll make an advanced guide after a few days.

Link to comment
Share on other sites

Thanks for the comments guys, i'll continue it on monday because im typing from my iPhone , so cant make it now. I'll create advanced one too, just to come back to home :/ thanks again for comments.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...